首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >-(UIAlertView *)alertView不被调用

-(UIAlertView *)alertView不被调用
EN

Stack Overflow用户
提问于 2010-03-15 15:26:08
回答 3查看 6.8K关注 0票数 8

我遇到了一个问题,UIAlertViewDelegate方法- (void)alertViewCancel:(UIAlertView *) AlertView 在取消带有cancel按钮的AlertView时没有被调用。

奇怪的是委托方法- (void)alertView:(UIAlertView *)alertView alertView非常有效。

有谁有主意吗?

提前感谢

肖恩

代码语言:javascript
复制
- (void)alertViewCancel:(UIAlertView *)alertView
{   
    if(![self aBooleanMethod])
    {
        exit(0);
    }
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    //some code
}   

当一个按钮被点击时,我称之为:

代码语言:javascript
复制
- (void)ImagePickDone
{
    UIAlertView *alertDone = [[UIAlertView alloc] 
                          initWithTitle:@"Done" 
                          message:@"Are u sure?"
                          delegate:self 
                          cancelButtonTitle:@"Cancel" 
                          otherButtonTitles: @"Yes", nil];
    [alertDone show];   
    [alertDone release];
}
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2010-03-15 15:50:16

alertViewCancel用于系统关闭警报视图时,而不是当用户按下“取消”按钮时。来自苹果文档的报价

或者,您可以实现alertViewCancel:方法,以便在系统取消警报视图时采取适当的操作。如果委托不实现此方法,则默认行为是模拟用户单击“取消”按钮并关闭视图。

如果您想在用户按下“取消”按钮时捕获,则应该使用clickedButtonAtIndex方法并检查索引是否对应于“取消”按钮的索引。要获得此索引,请使用:

代码语言:javascript
复制
index = alertDone.cancelButtonIndex;
票数 12
EN

Stack Overflow用户

发布于 2010-03-16 15:18:22

可以在此委托的索引0处处理Cancel:

代码语言:javascript
复制
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0){
      //cancel button clicked. Do something here.
    }
    else{
      //other button indexes clicked
    }
}   
票数 2
EN

Stack Overflow用户

发布于 2013-10-17 18:49:29

这可以通过两种方式加以改进。首先,它只处理用户实际单击按钮的情况。它不处理myAlert dismissWithClickedButtonIndex:被调用的情况,也不处理以其他方式解除警报的情况。其次,按钮0不一定是“取消”按钮。在有两个按钮的警报中,左边的是索引0,右边的是索引1。如果您更改标题,使右按钮显示“取消”,那么按钮1在逻辑上就是“取消”按钮。您可以实现"willDismiss“而不是"didDismiss”,该“didDismiss”将在对话框消失后调用,而不是在之前。

代码语言:javascript
复制
- (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == alertView.cancelButtonIndex)
    {
      //cancel button clicked. Do something here.
    }
    else
    {
      //other button indexes clicked
    }
}   
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2448244

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档