viewWillDisappear 内的 UIAlertView 未调用 clickedButtonAtIndex

发布于 2024-12-12 02:15:17 字数 1171 浏览 0 评论 0原文

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

I have the following code in a class, although the alert does appear in the UI (using iOS SDK 5.0) when the view is about to disappear, the clickedButtonAtIndex method is never called and the app terminates with "EXC_BAD_ACCESS". I validated the view does/is using my class as a delegate.

The code is on the main thread and after looking at all the other responses on this subject I fail to see why my delegate method is never called. I could use another clue folks.

  @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);
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

最冷一天 2024-12-19 02:15:17

警报视图不会阻塞 - 本质上,当您选择一个选项时,您的视图控制器已经看到 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 即可。

The alert view does not block - essentially by the time you choose an option, your view controller has seen viewWillDisappear, viewDidDisappear, and probably dealloc, meaning it doesn't exist anymore. Assuming you're using a UINavigationController, if the idea is to prompt the user before navigating back, you should override

- (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];
    }
}

in your UINavigationBar where ExitConfirmDelegate is a protocol with BOOL shouldConfirmExit. Your view controller would implement this protocol and return 'NO' if a pending alert view is visible. Then, when the user does click an option, simply call popViewControllerAnimated again from your clickedButtonAtIndex method.

り繁华旳梦境 2024-12-19 02:15:17

确保在调用“clickedButtonAtIndex”方法之前未释放 ConnectionViewController。

Make sure ConnectionViewController is not released before the "clickedButtonAtIndex" method is called.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文