【问题标题】:UIAlertView inside viewWillDisappear is not calling clickedButtonAtIndexviewWillDisappear 中的 UIAlertView 没有调用 clickedButtonAtIndex
【发布时间】:2011-10-25 21:11:39
【问题描述】:

我在一个类中有以下代码,虽然当视图即将消失时警报确实出现在 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);
    }

【问题讨论】:

    标签: ios5 uialertview


    【解决方案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。

    【讨论】:

    • 我知道这一定与阻塞有关,谢谢!
    【解决方案2】:

    确保调用“clickedButtonAtIndex”方法之前没有释放ConnectionViewController。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多