另一个视图控制器上的UIAlertView崩溃问题 [英] UIAlertView crash issue on another view controller

查看:134
本文介绍了另一个视图控制器上的UIAlertView崩溃问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个视图控制器,ViewController 1(VC1)和2(VC2).在VC2中,我有一个后退和完成按钮.单击后退按钮可直接转到VC1,完成后将进行api调用,当它收到响应时将显示警报视图,单击确定"将返回VC1.现在,当我进行api调用时,加载栏会显示,当我收到响应并显示AlertView时,它会消失.但是,如果在不到一秒的时间内,如果加载消失,并且如果我单击后退视图时将弹出AlertView,并且视图变为VC1,则警报将显示在VC1上并导致崩溃.

I have 2 view controllers, ViewController 1(VC1) and 2(VC2). In VC2 I have a back and done button. On clicking back button it goes directly to VC1 and on done it makes an api call and when it gets a response it shows an alert view and clicking ok goes back to VC1. Now when I make a api call a loading bar shows up and disappears when I get response and shows the AlertView. But if during that fraction of second when the loading disappears and AlertView is going to be popped up if I click on back and the view changes to VC1, the alert appears on VC1 and results in a crash.

这是一种罕见的情况,因为没有用户会故意尝试它,但是我想知道是否可以在不禁用后退按钮的情况下管理该崩溃.我认为可能还有其他情况,例如我们正在进行异步调用,是否允许用户在等待响应时使用UI,以及是否可能在另一个ViewController上显示任何错误警报,例如导致崩溃,因为警报所引用的委托是以前的视图控制器的委托.那么有什么方法可以有效地处理这种崩溃吗?

This is a rare case as no user will purposely try for it but I was wondering if that crash can be managed without disabling the back button. I think there can be other instance such cases like if we are making an asynchronous calls and if the user is allowed to use UI while waiting for response and if any error alert that was suppose to show on one ViewController shows up in another may result in crash since the delegate that alert is referring to is that of the previous view controller. So is there any way to handle this kind of crash efficiently?

//Alert View sample
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:[[message objectAtIndex:1] capitalizedString] message:[message objectAtIndex:0] delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil] ;
[alert setTag:701];
[alert show];

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if ([alertView tag] == 701)
        if (buttonIndex == 0)
        {
            [self.navigationController popViewControllerAnimated:YES];
        }


}

推荐答案

解决此问题的正确方法是使用实​​例变量保留对警报视图的引用.

The proper way to fix this problem is to use an instance variable to keep a reference to the alert view.

应在alertView:didDismissWithButtonIndex:委托方法中将此实例变量设置为nil.

This instance variable should be set to nil in the alertView:didDismissWithButtonIndex: delegate method.

在视图控制器的dealloc方法中,如果仍然设置了实例变量,则调用dismissWithClickedButtonIndex:animated:.

In the view controller's dealloc method, you call dismissWithClickedButtonIndex:animated: if the instance variable is still set.

假设_alertView是实例变量.

创建警报:

_alertView = [[UIAlertView alloc] initWithTitle:[[message objectAtIndex:1] capitalizedString] message:[message objectAtIndex:0] delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil] ;
[_alertView setTag:701];
[_alertView show];

更新您现有的alertView:clickedButtonAtIndex:方法:

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if ([alertView tag] == 701) {
         _alertView.delegate = nil;            
         _alertView = nil;
        if (buttonIndex == 0) {
            [self.navigationController popViewControllerAnimated:YES];
        }
    }
}

添加:

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
    _alertView = nil;
}

添加:

- (void)dealloc {
    if (_alertView) {
        _alertView.delegate = nil;
        [_alertView dismissWithClickedButtonIndex:_alertView.cancelButtonIndex animated:NO];
        _alertView = nil;
    }
}

这篇关于另一个视图控制器上的UIAlertView崩溃问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆