UIAlertView not showing

一个人想着一个人 提交于 2019-12-13 06:32:55

问题


I have a small problem with one of my UIAlertViews. I'm trying to show it, do some tasks, and then dismiss automatically. This is the code I'm using currently:

callingTaxi = [[UIAlertView alloc] initWithTitle:@"" message:@"検索中" delegate:nil cancelButtonTitle:nil otherButtonTitles:nil, nil];
[callingTaxi show];
/* Do some tasks */
[callingTaxi dismissWithClickedButtonIndex:0 animated:YES];
[callingTaxi release];

However, the UIAlertView only shows half-way. I can see the background darken, but then after the tasks have completed, the alert view quickly appears and then disappears, again.

Any ideas how to solve this issue?

Ben


回答1:


It does show, but you dismiss it right away, instead of waiting for the user to do something, with

[callingTaxi dismissWithClickedButtonIndex:0 animated:YES];

There is no time for iOS to render it completely. Anyway, this is not how dismissWithClickedButtonIndex is supposed to be used. What are you trying to achieve here?

Edit: I guess you need to assign a delegate to the UIAlertView, and let the delegate handle what happens inside the AlertView.




回答2:


You should not close it inside the same function which shows it, close it by timer, or as reaction to another event.




回答3:


You don't need a delegate. The problem is that the tasks you do must happen on a background thread, so the main thread is left alone to update the screen.

If you update your code to use blocks and dispatch queues, everything will work:

callingTaxi = [[UIAlertView alloc] initWithTitle:@"" message:@"検索中" delegate:nil cancelButtonTitle:nil otherButtonTitles:nil, nil];
[callingTaxi show];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, NULL), ^{
    /* Do some tasks */
    dispatch_async(dispatch_get_main_queue(), ^{
        // this code is back on the main thread, where it's safe to mess with the GUI
        [callingTaxi dismissWithClickedButtonIndex:0 animated:YES];
        [callingTaxi release];
    });
});



回答4:


you should create a method for the alertview

- (void) alertView: (UIAlertView *) alertView didDismissWithButtonIndex:(NSInteger)buttonIndex{
// Do stuff
// Or if you have multiple buttons, you could use a switch

[callingTaxi release];

Or you could Autorelease..

But x3ro has given the correct answer, you call the method yourself instead of waiting for the user to press the button..




回答5:


the uialertview is dismissed by: [callingTaxi dismissWithClickedButtonIndex:0 animated:YES]; before the user can read it.

How long do you intend to have the end user read it???



来源:https://stackoverflow.com/questions/4647175/uialertview-not-showing

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