Вопрос

I have a thread dispatched through GCD. When the thread completes, I present a UIAlertView to let the user know that the thread is complete. I execute the UIAlertView on the main thread ([NSThread mainThread]).

If I remain in the View Controller the alert view appears fine and when I press the [Ok] button it disappears as it should. However, if I return to the main view controller (thus popping the prior view controller from the stack) I get the above error message after clicking [Ok]. The AlertView displays properly and correctly closes.

Can I assume that this has nothing to do with the UIAlartView and that perhaps the issue is with some other code attempting to execute after the alartview? Even so, I can't see anything after that block of code that would be executing. I guess at this point, I'm just trying to confirm that if the AlertView works properly then the problem is not with that.

If I can simplify this to a small chunk of sample code I'll post that here.

Thanks!

Это было полезно?

Решение

Most likely you set the alert view's delegate to self (the view controller). So if the view controller is dismissed before the alert view, the alert view tries to contact its (now reallocated) delegate.

Simply pass nil to the alert view delegate instead of self. This assumes you don't actually need to handle the OK button.

Другие советы

To use the delegate and avoid the EXC_BAD_ACCESS, you can add a property like

@property (nonatomic, weak) UIAlertView *myAlertView;

to your view controller, and assign the alert view to it. Then, in your view controller's dealloc method add

- (void)dealloc {
    // other dealloc code
    if (self == self.myAlertView.delegate) self.myAlertView.delegate = nil;
}
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top