简体   繁体   English

通过点击外部或取消按钮来关闭iOS中的警报视图

[英]Dismiss alert view in iOS by tapping outside or cancel button

I had a very strange problem, I want to dismiss the UIAlertView when the user either taps on cancel button or anywhere else. 我有一个非常奇怪的问题,当用户点击“取消”按钮或其他任何地方时,我想关闭UIAlertView I had read almost all the post and couldn't find an answer that solved my problem do any of you have any idea how to do this? 我已经看了几乎所有的帖子,找不到解决我问题的答案,你们中的任何人都知道如何做到这一点吗? Please guide me through the steps, I will be very thankful for this. 请引导我完成所有步骤,对此我将非常感谢。

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:label.text
                                                message:label1.text
                                               delegate:self
                                      cancelButtonTitle:@"ཕྱིར་འཐེན།"
                                      otherButtonTitles:@"ཉེ་ཆར།",@"དགའ་མོས།",@"ཉེ་ཆར་གཙང་བཟོ།",@"དགའ་མོས་གཙང་བཟོ།",nil];
[alert show];

Call touchesEnded method. 调用touchesEnded方法。 It'll help you dismiss alertView when user tap anywhere else on the screen 当用户点击屏幕上的任何其他位置时,它将帮助您关闭alertView

-(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.yourAlert dismissWithClickedButtonIndex:0 animated:YES];
}

and remember to create yourAlert as strong reference 并记住创建您的警报作为有力的参考

Try this code 试试这个代码

// Add gesture
UITapGestureRecognizer *tapOnView = [[UITapGestureRecognizer alloc] initWithTarget:self
                                                                            action:@selector(dismissAlert)];
[alert addGestureRecognizer:tapOnView];

Then you can call a function that dismiss your alert 然后,您可以调用取消警报的功能

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM