viewWillDisappear里面的UIAlertView没有调用clickedButtonAtIndex

时间:2011-10-25 21:11:40

标签: ios5 uialertview

我在类中有以下代码,虽然当视图即将消失时,警报确实出现在UI中(使用iOS SDK 5.0),但从未调用 clickedButtonAtIndex 方法终止于“EXC_BAD_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);
    }

2 个答案:

答案 0 :(得分:1)

警报视图不会阻止 - 主要是在您选择选项时,您的视图控制器已经看到viewWillDisappear,viewDidDisappear和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];
    }
}
在你的UINavigationBar中的

,其中ExitConfirmDelegate是一个BOOL的协议shouldConfirmExit。您的视图控制器将实现此协议,如果可以看到挂起的警报视图,则返回“NO”。然后,当用户单击某个选项时,只需从clickedButtonAtIndex方法再次调用popViewControllerAnimated。

答案 1 :(得分:0)

确保在调用“clickedButtonAtIndex”方法之前未发布ConnectionViewController。