简体   繁体   中英

iOS 5, ARC: UIAlertView doesn't work in @catch block

-(IBAction)showCountryInfo:(id)sender
{
@try 
{
    CountryProperties *countryProperties=[self.storyboard instantiateViewControllerWithIdentifier:@"Culture"];
    countryProperties.countryID=self.countryID;
    countryProperties.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
    [self.navigationController presentModalViewController:countryProperties animated:YES];
}
@catch (NSException *exception) 
{
    UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Sorry" message:@"Module under revision" delegate:self cancelButtonTitle:@"Dismiss" otherButtonTitles:nil, nil];
    [alert show];
} 
}

I do expect from this code just show an alertView, and after user will press 'Dismiss' button the alertView should disappear. that's all. anyway, alert view doesn't work. if an exception does happen, the alert view shows up, but when I push dismiss button nothing happens and program freezing still.

Does the evil happens because I use my alertView inside the @catch block or something like that?

Thanx in advance.

What exactly inside the @try is throwing an exception? Exception handling in Objective-C is generally frowned upon for error handling. The Objective-C Programming Language docs say:

Exceptions are resource-intensive in Objective-C. You should not use exceptions for general flow-control, or simply to signify errors. Instead you should use the return value of a method or function to indicate that an error has occurred, and provide information about the problem in an error object.

The Exception Programming Topics guide has similar sentiment:

Important You should reserve the use of exceptions for programming or unexpected runtime errors such as out-of-bounds collection access, attempts to mutate immutable objects, sending an invalid message, and losing the connection to the window server. You usually take care of these sorts of errors with exceptions when an application is being created rather than at runtime.

The Error Handling Programming Guide is good reading, too.

Lots of drawing things have to happen on the main thread. You might find your answer if you move your alert view code to its own method, and in your @catch you call:

[self performSelectorOnMainThread:@selector(myAlertMethod) withObject:nil waitUntilDone:NO];

...where myAlertMethod is the method that you moved your alert code to.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM