我正在开发一个以编程方式创建弹出视图的应用。现在要求是当用户点击除popview之外的其他地方时,我想从superview中删除该视图。
以下是我创建视图的代码
- (IBAction)Morebtn:(id)sender {
UIView *cv = [[UIView alloc]initWithFrame:CGRectMake(200, 60, 100, 80)];
UIButton *label1 = [[UIButton alloc]initWithFrame:CGRectMake(-50,2, 200, 30)];
[label1 setTitle: @"My Button" forState: UIControlStateNormal];
label1.titleLabel.font=[UIFont fontWithName:@"SegoeUI" size:12.0];
[label1 addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[cv addSubview:label1];
UILabel *label2 = [[UILabel alloc]initWithFrame:CGRectMake(5,20, 200, 30)];
label2.text = @"Mark as unread";
label2.font=[UIFont fontWithName:@"SegoeUI" size:12.0];
[cv addSubview:label2]; //add label2 to your custom view
[cv setBackgroundColor:[UIColor grayColor]];
[self.view addSubview:cv];
}
这是我的截屏视图
答案 0 :(得分:2)
在全局
中声明UIView *cv
和Remove
功能就像
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[cv setHidden:YES];
[self.view.superview endEditing:YES];
}
选择否2
@property (strong, nonatomic) UIView * cv;
-(void)viewDidLoad
{
[super viewDidLoad];
// here add the view
cv = [[UIView alloc]initWithFrame:CGRectMake(200, 60, 100, 80)];
UIButton *label1 = [[UIButton alloc]initWithFrame:CGRectMake(-50,2, 200, 30)];
[label1 setTitle: @"My Button" forState: UIControlStateNormal];
label1.titleLabel.font=[UIFont fontWithName:@"SegoeUI" size:12.0];
[label1 addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[cv addSubview:label1];
UILabel *label2 = [[UILabel alloc]initWithFrame:CGRectMake(5,20, 200, 30)];
label2.text = @"Mark as unread";
label2.font=[UIFont fontWithName:@"SegoeUI" size:12.0];
[cv addSubview:label2]; //add label2 to your custom view
[cv setBackgroundColor:[UIColor grayColor]];
}
按钮操作
- (IBAction)Morebtn:(id)sender {
[self.view addSubview:cv];
}
答案 1 :(得分:1)
100%
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
[super touchesBegan:touches withEvent:event];
[urView removeFromSuperview];
}
答案 2 :(得分:1)
试试这个
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
CGPoint locationPoint = [[touches anyObject] locationInView:self.view];
UIView* viewYouWishToObtain = [self hitTest:locationPoint withEvent:event];
if ([viewYouWishToObtain isKindOfClass: [customView Class]])
[customView removeFromSuperview];
}
答案 3 :(得分:0)
希望这会对你有帮助..
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[cv removeFromSuperView];
//或
[cv setHidden:Yes];
}
答案 4 :(得分:0)
非常简单,只需在自己的UIViewController中创建一个方法。 touchesBegan并写入一个方法。 但在此之前,你的对象cv在你的视图中是全局的
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
[self.cv removeFromSuperview];
self.cv=nil;
}