以编程方式通过 uibutton 删除 uiview
Posted
技术标签:
【中文标题】以编程方式通过 uibutton 删除 uiview【英文标题】:remove uiview by uibutton programmatically 【发布时间】:2012-01-03 18:26:04 【问题描述】:我不知道如何通过用户单击 uibutton 来删除 uiview,一旦加载场景,uibutton 就会加载。 uibutton 也没有得到响应。 我应该如何设置它?请帮忙,非常感谢。
- (void)viewWillAppear:(BOOL)animated
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, 200, 100);
[removeSticker setImage:[UIImage imageNamed:@"cancel-disabled.png"] forState:UIControlStateNormal];
[removeSticker addTarget:holderView action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[holderView addSubview: removeSticker];
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];
[parentPreviewView addSubview:holderView];
- (void) buttonClicked: (id)sender
[self.view removeFromSuperview];
【问题讨论】:
【参考方案1】:将目标从holderView
更改为self
:
[removeSticker addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
并将删除视图的代码更改为以下内容:
[holderView removeFromSuperview];
【讨论】:
好答案,假设最初的问题是关于删除holderView
。我的一个建议是将buttonClicked
方法高于 viewWillAppear:
方法,特别是如果buttonClicked
未在.h 接口文件中声明。
@MichaelDautermann 我认为buttonClicked
方法定义在viewWillAppear
之上或之下并不重要,只要我们使用选择器即可。
感谢您的及时回复,但是当我将其更改为[holderView removeFromSuperview];
时,它说使用未声明的标识符?
是的,那是因为holderView
是一个局部变量。您可以将其设为私有变量,或者只使用以下代码:UIView *holderView = [(UIButton *)sender superView]; [holderView removeFromSuperview];
可能是我说的不够清楚,我在做一个照片装饰app,其实我是想把用户添加的装饰贴纸去掉,也就是imageView,所以我应该删除 imageView 而不是 holderView?非常感谢。【参考方案2】:
使用以下技术。指定您在视图中拥有的每个控件的标签。然后您可以从这个“标签”属性中受益
假设我将标签指定为 100。那么
UIButtom *buttonToRemove=(UIButton*)[self.view viewWithTag:100];
现在删除它(按钮)使用这个
[buttonToRemove removeFromSuperview];
【讨论】:
以上是关于以编程方式通过 uibutton 删除 uiview的主要内容,如果未能解决你的问题,请参考以下文章