在子视图中声明 uibutton
Posted
技术标签:
【中文标题】在子视图中声明 uibutton【英文标题】:Declare uibutton in a subview 【发布时间】:2012-01-05 17:57:13 【问题描述】:我不知道如何在 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];
这行应该怎么写?请帮忙,非常感谢。
【问题讨论】:
【参考方案1】:就这个
-(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。
【讨论】:
感谢您的回复,但我试过它说未声明的标识符 'removeSticker' 对不起,当我更新我的答案时,我应该明白这一点。再次查看更新。将 removeSticker 设为 ivar。 我是这里的新手,我应该如何使它成为 ivar?非常感谢! 我会更新我的答案,所以我可以使用这些 cmets 不支持的格式。 Jim,还有一个问题,我用bringSubviewToFront
代替holderView
,removeSticker
只出现在新添加的holderView
中,知道吗?【参考方案2】:
试试这个。
-(void)longPress:(id)sender
[(UIButton *)sender setHidden:NO];
在您的原始代码中,您引用了不同范围内的变量(即 holderView)。这就是错误的原因。
【讨论】:
对不起,我误会了。 longPress 来自 holderView,好吧。您要做的就是将removeSticker
声明为实例变量(而不是像您那样作为局部变量),然后调用 [removeSticker setHidden:NO]。以上是关于在子视图中声明 uibutton的主要内容,如果未能解决你的问题,请参考以下文章