Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS 6.1.2 - Why the UIAlertView not be dismissed? background still on the screen

here is my code:

UIAlertView *someError = [[UIAlertView alloc] initWithTitle:nil message:@"Server Error!" delegate:nil cancelButtonTitle: @"ok" otherButtonTitles: nil];
[someError show];

when I tap the ok button,the alert background still there,and I tap the screen,the status bar is flickering.

I found some answers,but they not work,I changed my code to:

dispatch_async(dispatch_get_main_queue(), ^{
       UIAlertView *someError = [[UIAlertView alloc] initWithTitle:nil message:@"Server Error!" delegate:nil cancelButtonTitle: @"ok" otherButtonTitles: nil];
       [someError show];
});

or set the Delegate:

dispatch_async(dispatch_get_main_queue(), ^{
       UIAlertView *someError = [[UIAlertView alloc] initWithTitle:nil message:@"Server Error!" delegate:self cancelButtonTitle: @"ok" otherButtonTitles: nil];
       [someError show];
});

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    dispatch_async(dispatch_get_main_queue(), ^{
        [alertView dismissWithClickedButtonIndex:buttonIndex animated:NO];
    });

}

it still not work,then I try to:

UIAlertView *someError = [[UIAlertView alloc] initWithTitle:nil message:@"error" delegate:nil cancelButtonTitle: @"ok" otherButtonTitles: nil];
[someError performSelectorOnMainThread:@selector(show) withObject:nil waitUntilDone:NO];

not work,crazy,this is not work too:

[self performSelectorOnMainThread:@selector(showAlert:) withObject:@"error", nil)  waitUntilDone:NO];

// the show Alert function
-(void)showAlert:(NSString*)str
{
    UIAlertView *someError = [[UIAlertView alloc] initWithTitle:nil message: str delegate: nil cancelButtonTitle: @"ok" otherButtonTitles: nil];
    [someError performSelectorOnMainThread:@selector(show) withObject:nil waitUntilDone:NO];
}

still not work.

before and after alert show:

before the alert showafter tap ok

like image 512
yellow Avatar asked Oct 05 '22 06:10

yellow


1 Answers

Make certain your view controller (or wherever you are creating your UIAlertView from) has "<UIAlertViewDelegate>" defined in the @interface line. For example:

@interface YellowViewController : UIViewController <UIAlertViewDelegate>

Now, when you create a UIAlertView from your subclassed view controller and set the delegate to self, your view controller should conform to and should receive the delegate protocol methods.

Next thing. This bit of code looks correct to me:

dispatch_async(dispatch_get_main_queue(), ^{
       UIAlertView *someError = [[UIAlertView alloc] initWithTitle:nil message:@"Server Error!" delegate:self cancelButtonTitle: @"ok" otherButtonTitles: nil];
       [someError show];
});

But your delegate method calls are going to happen on the main thread (which is where all the UI happens):

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    [alertView dismissWithClickedButtonIndex:buttonIndex YES];
}

In other words, get rid of the dispatch stuff in there and see what happens. Then again, the Apple documentation for "clickedButtonAtIndex:" says:

Discussion

The receiver is automatically dismissed after this method is invoked.

So you might not need to actually explicitly dismiss the alert view.

like image 128
Michael Dautermann Avatar answered Oct 13 '22 12:10

Michael Dautermann