Take the 2-minute tour ×

I'm stuck at how to declare the uibutton in uiview:

- (void)viewWillAppear:(BOOL)animated {

NSLog(@"Added holderView.");
UIView *holderView = [[UIView alloc] initWithFrame:CGRectMake(0, 50, _imagePicker.selectedImage.size.width, _imagePicker.selectedImage.size.height)];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:[holderView frame]];
[imageView setImage:_imagePicker.selectedImage];
[holderView addSubview:imageView];

UIButton *removeSticker = [UIButton buttonWithType:UIButtonTypeCustom];
removeSticker.frame = CGRectMake(0, 0, 100, 100);
[removeSticker setImage:[UIImage imageNamed:@"cancel-disabled.png"] forState:UIControlStateNormal];
[removeSticker addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[holderView addSubview: removeSticker];
[removeSticker setHidden: YES];

UIPinchGestureRecognizer *pinchRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(scale:)];
[pinchRecognizer setDelegate:self];
[holderView addGestureRecognizer:pinchRecognizer];

UIRotationGestureRecognizer *rotationRecognizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotate:)];
[rotationRecognizer setDelegate:self];
[holderView addGestureRecognizer:rotationRecognizer];

UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(move:)];
[panRecognizer setMinimumNumberOfTouches:1];
[panRecognizer setMaximumNumberOfTouches:1];
[panRecognizer setDelegate:self];
[holderView addGestureRecognizer:panRecognizer];

UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapped:)];
[tapRecognizer setNumberOfTapsRequired:1];
[tapRecognizer setDelegate:self];
[holderView addGestureRecognizer:tapRecognizer];

[longPressRecognizer setDelegate:self];
[holderView addGestureRecognizer:longPressRecognizer];

[stickerView addSubview:holderView];


- (void)buttonClicked:(id)sender {
UIView *holderView = [(UIButton *)sender superview]; [holderView removeFromSuperview];

Here I want to make the uibutton *removeSticker visible to be able to do buttonClicked, however it said undeclared identifier *holderView:

-(void)longPress:(id)sender {

for(UIButton *removeSticker in holderView.subviews)[removeSticker setHidden: NO];


How should I write this line? Please help, thanks a lot.

share|improve this question
add comment

2 Answers

up vote 0 down vote accepted

Just this

 -(void)longPress:(id)sender {
    [removeSticker setHidden:NO];

The reason you get that error is that the scope for holderView is limited to the block it is declared in. For future reference, if you want it to have scope across your entire class, it should be an ivar or property.

You also want to make removeSticker an ivar for the same reason.

To make it an ivar, put the declaration in the header file:

UIButton *removeSticker;

Then in your viewWillAppear code you change this line

UIButton *removeSticker = [UIButton buttonWithType:UIButtonTypeCustom];

to this

removeSticker = [UIButton buttonWithType:UIButtonTypeCustom];

But barley has it right in his answer, too. sender is already pointing to the removeSticker object, whether you declare it as an ivar or not.

share|improve this answer
thanks for your reply but I've tried that it said undeclared identifier 'removeSticker' –  zeropt7 Jan 5 '12 at 18:07
Sorry, I should have caught that with when I updated my answer. See the update again. Make removeSticker an ivar. –  Jim Jan 5 '12 at 18:09
I'm a newbie here, how should I make it an ivar? Thanks a lot! –  zeropt7 Jan 5 '12 at 18:11
I'll update my answer, so I can use formatting that these comments dont' support. –  Jim Jan 5 '12 at 18:14
Thanks a lot, it works. –  zeropt7 Jan 5 '12 at 18:26
show 2 more comments

Try this.

-(void)longPress:(id)sender {
    [(UIButton *)sender setHidden:NO];

In your original code, you are referencing a variable in different scope (i.e., holderView). Thats why the error.

share|improve this answer
thanks a lot but sorry but this make the app crashes –  zeropt7 Jan 5 '12 at 18:20
Ah sorry, I misunderstood. The longPress is coming from holderView, okay. What you have to do is to declare removeSticker as an instance variable (not as a local variable as you do), and call [removeSticker setHidden:NO]. –  barley Jan 5 '12 at 18:23
Problem solved, thanks a lot. –  zeropt7 Jan 5 '12 at 18:26
add comment

Your Answer


