简体   繁体   English

iOS 5,ARC:UIAlertView在@catch块中不起作用

[英]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. 我确实希望此代码仅显示一个alertView,并且在用户按下“关闭”按钮后,alertView应该消失。 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? 是因为我在@catch块之内使用了alertView还是类似的东西发生了邪恶吗?

Thanx in advance. 提前感谢。

What exactly inside the @try is throwing an exception? @try内部到底是什么引发异常? Exception handling in Objective-C is generally frowned upon for error handling. 对于错误处理,通常不赞成使用Objective-C中的异常处理。 The Objective-C Programming Language docs say: Objective-C编程语言文档说:

Exceptions are resource-intensive in Objective-C. 在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: 如果将警报视图代码移至其自己的方法,然后在@catch中调用,则可能会找到答案:

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

...where myAlertMethod is the method that you moved your alert code to. ...其中myAlertMethod是将警报代码移至的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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