Why isn't UIAlertView Showing?

走远了吗. 提交于 2019-11-30 17:31:11

问题


For some reason screen gets dark and freezes, alert is not shown... can someone please help?

Thanks in advance!

} else {
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Hello!" 
                                                message:@"Hello!" delegate:self 
                                      cancelButtonTitle:@"Done" 
                                      otherButtonTitles:nil];
    [alert show];
    [alert release];
}

回答1:


You are probably calling show from a background thread, call it on the main thread like this:

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Hello!" 
                                            message:@"Hello!" delegate:self 
                                            cancelButtonTitle:@"Done" 
                                            otherButtonTitles:nil];
[alert performSelectorOnMainThread:@selector(show)
                            withObject:nil
                            waitUntilDone:NO];
[alert release];



回答2:


Delegate is correct, but maybe because your do a release at the end it may cause a problem.

Try with a nil delegate :-)

For example :

UIAlertView *alertView;
alertView = [ [ UIAlertView alloc ] init ];
[ alertView setMessage:@"Hello World" ];
[ alertView show ];
[ alertView release ];

If it works, then it was the delegate and you need to declare the variable as a class var. Or it maybe be elsewhere.




回答3:


Is this alert maybe sitting in a big loop and you are not running on multiple threads? The screen darkening and nothing happening is something I equate with running a long process on the main thread (so the UI doesn't refresh and show the alert).




回答4:


You get a dark screen without a popup, or slower popup if you show the UIAlertView from a background thread. Just out it back in the main thread and it will be fine. I just had this problem last week.



来源:https://stackoverflow.com/questions/5331724/why-isnt-uialertview-showing

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