Ios5 UIAlertView inside ViewWillEnglish未调用ClickedButtonIndex

Ios5 UIAlertView inside ViewWillEnglish未调用ClickedButtonIndex,ios5,uialertview,Ios5,Uialertview,我在一个类中有以下代码,尽管当视图即将消失时,警报确实会出现在UI中(使用iOS SDK 5.0),但不会调用ClickedButtonIndex方法,并且应用程序会以“EXC\u BAD\u ACCESS”终止。我验证了视图是否将我的类用作委托 代码位于主线程上,在查看了关于此主题的所有其他响应后,我无法理解为什么从未调用我的委托方法。我需要另一条线索 @interface ConnectionViewController : UIViewController <UIAlertVie

我在一个类中有以下代码,尽管当视图即将消失时,警报确实会出现在UI中(使用iOS SDK 5.0),但不会调用ClickedButtonIndex方法,并且应用程序会以“EXC\u BAD\u ACCESS”终止。我验证了视图是否将我的类用作委托

代码位于主线程上,在查看了关于此主题的所有其他响应后,我无法理解为什么从未调用我的委托方法。我需要另一条线索

  @interface ConnectionViewController : UIViewController <UIAlertViewDelegate> {
           ....
    }

@implementation ConnectionViewController
...

    - (void)viewWillDisappear:(BOOL)animated
    {
        connection = [Connection objectWithConnName:[connectionName text] host:[mtDevice text] user:[userName text] passwd:[userPassword text]];
        BOOL result = [connection test];
        if (result) {
            [[FirstViewController sharedInstance] addConnection:connection];    
        } else {
            UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"No Connection" message:@"Failed to connect to device" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Ignore", @"Ok", nil];
            [alert show];
        }
    }

    - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
    {
        NSLog(@"clickedButtonAtIndex: %d",buttonIndex);
    }
@接口连接ViewController:UIViewController{
....
}
@实现连接视图控制器
...
-(无效)视图将消失:(BOOL)已设置动画
{
connection=[connection objectWithConnName:[connectionName text]主机:[mtDevice text]用户:[用户名文本]密码:[用户密码文本]];
BOOL结果=[连接测试];
如果(结果){
[[FirstViewController sharedInstance]添加连接:连接];
}否则{
UIAlertView*alert=[[UIAlertView alloc]initWithTitle:@“无连接”消息:@“无法连接到设备”委托:自取消按钮:@“取消”其他按钮:@“忽略”,“确定”,无];
[警报显示];
}
}
-(无效)alertView:(UIAlertView*)alertView单击按钮索引:(NSInteger)按钮索引
{
NSLog(@“ClickedButtonIndex:%d”,buttonIndex);
}

在调用“ClickedButtonIndex”方法之前,确保未释放ConnectionViewController

警报视图不会阻塞-基本上在您选择选项时,您的视图控制器已看到ViewWillEnglish、ViewDidEnglish和dealloc,这意味着它不再存在。假设您使用的是UINavigationController,如果您的想法是在返回导航之前提示用户,那么您应该覆盖它

- (UINavigationItem *)popNavigationItemAnimated:(BOOL)animated;
{
    MyAppDelegateName* delegate = (MyAppDelegateName*)[[UIApplication sharedApplication] delegate];
    if([delegate.navigationController.topViewController conformsToProtocol:@protocol(ExitConfirmDelegate)]) {
       if([(UIViewController<ExitConfirmDelegate>*)delegate.navigationController.topViewController shouldConfirmExit]) {
            return;
       }
       [delegate.navigationController popViewControllerAnimated:animated];
    }
}
-(UINavigationItem*)PopNavigationItem动画:(BOOL)动画;
{
MyAppDelegateName*委托=(MyAppDelegateName*)[[UIApplication sharedApplication]委托];
if([delegate.navigationController.topViewController conformsToProtocol:@protocol(ExitConfigrmDelegate)]){
如果([(UIViewController*)delegate.navigationController.topViewController shouldConfirmExit]){
返回;
}
[delegate.navigationController PopViewController动画:动画];
}
}

在UINavigationBar中,ExitConfigDelegate是一个带有BOOL shouldConfirmExit的协议。您的视图控制器将实现此协议,如果挂起的警报视图可见,则返回“否”。然后,当用户确实单击某个选项时,只需通过ClickedButtonIndex方法再次调用popViewControllerAnimated即可。

我知道这一定与阻塞有关,谢谢!