alertView:didDismissWithButtonIndex:发送到解除分配实例的消息 [英] alertView:didDismissWithButtonIndex: message sent to deallocated instance

查看:295
本文介绍了alertView:didDismissWithButtonIndex:发送到解除分配实例的消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题只发生在iOS4.3上。我正在使用ARC而我的Base SDK是iOS6。

The problem is only happening with iOS4.3. I'm using ARC and my Base SDK is iOS6.

在我的视图控制器的 -viewDidAppear 中,我检查如果这是第一次启动应用程序,如果是,那么我创建并显示UIAlertView。我将UIAlertView分配给视图控制器上的 strong 属性,并将self设置为UIAlertView委托。

In -viewDidAppear of my view controller, I check if this is the first time the app has been started and if so, then I create and show a UIAlertView. I assign that UIAlertView to a strong property on the view controller and set self as the UIAlertView delegate.

self.uiAlertView = [[UIAlertView alloc] initWithTitle:@"Welcome!"
                                              message:messageString 
                                             delegate:self
                                    cancelButtonTitle:@"OK"
                                    otherButtonTitles:@"View Tutorial Videos", @"Email Support", nil];

当我点击其中一个按钮时,应用程序崩溃抱怨 -alertView :didDismissWithButtonIndex:已发送到已解除分配的实例。委托是显示UIAlertView的视图控制器。

When I tap one of the buttons, the app crashes complaining that -alertView:didDismissWithButtonIndex: was sent to a deallocated instance. The delegate is the view controller that is displaying the UIAlertView.

在应用程序的所有后续启动中,当未显示UIAlertView时没有问题。视图控制器肯定没有被释放。

On all subsequent launches of the app, when the UIAlertView isn't shown there are no problems. The view controller is definitely not being deallocated.

如果我显示UIAlertView但将委托设置为nil,那么没有问题,应用程序继续工作,所以很明显视图控制器尚未被释放,因为我可以继续使用它。

If I display the UIAlertView but set the delegate to nil, then there is no problem and the app continues working, so clearly the view controller hasn't been deallocated because I can keep using it.

发生了什么?这只会导致iOS4.3出现问题。

What is happening? This only causes a problem with iOS4.3.

编辑:根据评论中的建议,我在不同的地方添加了更多日志消息。

Based on suggestions in the comments, I added some more log messages in different places.

I已经发现视图控制器正在被取消分配,但仅当该视图控制器显示UIAlertView时。世界上的什么会导致视图控制器被释放,因为它将自己设置为UIAlertView的委托然后显示它?

I've discovered that the view controller IS getting dealloc'd, but only if that view controller displays the UIAlertView. What in the world would cause the view controller to get dealloc'd just because it sets itself as the delegate of a UIAlertView and then displays it?

我的应用代表有一个 strong 对视图控制器的引用,因此绝对没有理由让我看到视图控制器被dealloc'd。

My app delegate has a strong reference to the view controller, so there is absolutely no reason that I can see for the view controller to get dealloc'd.

编辑2:我发现在启动期间我的主视图控制器正在实例化TWICE。第一个是创建UIAlertView的那个,并且正在进行dealloc'd。第二个是我之后能够与之交互的那个,这让我觉得视图控制器仍然存在且可操作。

EDIT 2: I've discovered that during start up my main view controller is being instantiated TWICE. The first one is the one creating the UIAlertView and that one is getting dealloc'd. The second one is the one that I've been able to interact with afterwards that made me think the view controller was still there and operable.

然而,我无法弄清楚我的视图控制器将被创建两次的原因或原因。我没有视图控制器的任何alloc / init语句。它只存在于MainWindow_iPhone.xib中。

However, I can't figure out where or why my view controller would be created twice. I don't have any alloc/init statements for the view controller. It only exists in the MainWindow_iPhone.xib.

第一次在我的视图控制器上调用viewDidLoad时,上面的堆栈帧是[UIViewController视图]。在我的视图控制器的第二个实例上第二次调用viewDidLoad,上面的堆栈帧是[UINib instantiateWithOwner:options:]

The first time viewDidLoad is called on my view controller, the stack frame above is [UIViewController view]. The second time viewDidLoad is called on the second instance of my view controller, the stack frame above is [UINib instantiateWithOwner:options:]

编辑3:我已解决了这个问题,但我不明白为什么会这样。也许你可以帮我理解。

EDIT 3: I've "fixed" the problem, but I don't understand why this would happen. Perhaps you can help me understand.

在我的MainWindow_iPhone.xib中,我创建了我的根视图控制器并将其分配给我的app委托上的IBOutlet。相反,我从xib中删除了视图控制器,并在 -application :( UIApplication *)应用程序中的代码中创建了它:didFinishLaunchingWithOptions:(NSDictionary *)launchOptions ...和问题消失了。

In my MainWindow_iPhone.xib, I created my root view controller and assigned it to an IBOutlet on my app delegate. Instead, I deleted the view controller from the xib and created it in code in the -application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions ... and the problem disappeared.

为什么在xib中视图控制器会被创建两次?

Why in the world would the view controller be created twice when in the xib?

推荐答案

我之前有这个问题。 alertView:didDismissWithButtonIndex:在alertView:clickedButtonAtIndex:之后调用。您最有可能通过执行[self.navigationController popViewControllerAnimated:YES]之类的操作来取消分配alertView中的视图控制器:clickedButtonAtIndex。

I have this issue before. alertView:didDismissWithButtonIndex: is called after alertView: clickedButtonAtIndex:. You most likely deallocate the view controller in alertView:clickedButtonAtIndex by doing something like [self.navigationController popViewControllerAnimated:YES].

UIAlertView委托分配的不是弱引用。取消分配委托时,它不会自动设置为nil。这就是你的代码崩溃的原因。

UIAlertView delegate is assign not weak reference. When delegate is deallocated, it is not set to nil automatically. That's reason why your code is crashed.

这篇关于alertView:didDismissWithButtonIndex:发送到解除分配实例的消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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