UIAlertView:点击警报框外部时关闭 UIAlertView [重复]
Posted
技术标签:
【中文标题】UIAlertView:点击警报框外部时关闭 UIAlertView [重复]【英文标题】:UIAlertView:Dismiss UIAlertView on click of out side the alert box [duplicate] 【发布时间】:2013-03-21 05:57:38 【问题描述】:您好,我是 Xcode 的新手,n Objective-C 在这方面需要帮助。 我有一个无按钮 UIAlertView,它会在点击自身时被关闭。但我只想在点击警报框外部而不是里面时关闭它。谢谢
//generating the alertview with no button
+(void) showAlertNoButtons:(NSString*)title text:(NSString*)text
UIAlertView* alertView = [[UIAlertView alloc]
initWithTitle:title message:text
delegate:self cancelButtonTitle:nil
otherButtonTitles:nil];
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc]
initWithTarget:self
action:@selector(dismissAlert:)];
alertView.delegate=self;
[alertView addGestureRecognizer:singleTap];
[alertView setMultipleTouchEnabled:YES];
[alertView setUserInteractionEnabled:YES];
[alertView show];
//
+(void)dismissAlert:(UITapGestureRecognizer *)gesture
UIAlertView* alertView =(UIAlertView *)gesture.view;
[alertView dismissWithClickedButtonIndex:0 animated:YES];
【问题讨论】:
***.com/questions/8260562/… 【参考方案1】:您需要致电- (void)dismissWithClickedButtonIndex:(NSInteger)buttonIndex animated:(BOOL)animated
当你点击其他任何地方时
这段代码将处理您的点击
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
[self dismissWithClickedButtonIndex:index animated:YES];
【讨论】:
[self dismissWithClickedButtonIndex:-1 animated:YES]
如果您不想让点击在解除警报时执行任何警报功能。以上是关于UIAlertView:点击警报框外部时关闭 UIAlertView [重复]的主要内容,如果未能解决你的问题,请参考以下文章