在子视图中声明uibutton

时间:2012-01-05 17:57:14

标签: ios uiview uibutton subview

我坚持如何在uiview中声明uibutton:

- (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];

UILongPressGestureRecognizer *longPressRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
[longPressRecognizer setDelegate:self];
[holderView addGestureRecognizer:longPressRecognizer];

[stickerView addSubview:holderView];

}

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

在这里,我想让uibutton * removeSticker可见,以便能够执行buttonClicked,但是它表示未声明的标识符* holderView:

-(void)longPress:(id)sender {

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

}

我应该怎么写这一行?请帮助,非常感谢。

2 个答案:

答案 0 :(得分:0)

就是这个

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

您收到该错误的原因是holderView的范围仅限于声明它的块。为了将来参考,如果您希望它在整个类中具有范围,那么它应该是一个ivar或财产。

出于同样的原因,你也想让removeSticker成为一个ivar。

要使其成为ivar,请将声明放在头文件中:

UIButton *removeSticker;

然后在viewWillAppear代码中更改此行

UIButton *removeSticker = [UIButton buttonWithType:UIButtonTypeCustom];

到这个

removeSticker = [UIButton buttonWithType:UIButtonTypeCustom];

但是大麦在他的回答中也是对的。 sender已指向removeSticker对象,无论您是否将其声明为ivar。

答案 1 :(得分:0)

试试这个。

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

在原始代码中,您引用了不同范围的变量(即holderView)。这就是错误的原因。