iOS 7 bug or my bug in UIAlertView

我与影子孤独终老i 提交于 2019-11-27 07:48:39

Stupid me, I just have to set the alert view's delegate to nil

Hui Chen

Because the delegate of the UIAlertView is an assign property in UIAlertView. I think that it is Apple's fault. It should be a weak pointer in ARC. But it is an assign property so you need to set the any alert view's delegate to nil before destroying the delegate (most of the time a controller class got popped or navigated back). Read the .h file for UIAlertView about the delegate, you can find it is an assign property and someone commented after the declaration saying "//weak reference".

The best way to avoid problem with UIAlertView while using delegation is to make instance of UIAlertView as iVar for delegate class. And after you should make delegate property of alertView as nil in dealloc of delegate class

@implementation YOUR_CLASS
{
    UIAlertView *_alert;
}

- (void)dealloc
{
    _alert.delegate = nil;
}


- (void)showAlertView
{
    _alert = [[UIAlertView alloc] initWithTitle:title message:message delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alert show];
}

The same approach is good enough for all old classes with assign-type delegates.

If you need to specify a delegate, doing a 'autorelease' on the alert view will also work.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!