繁体   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];
} 
}

我确实希望此代码仅显示一个alertView,并且在用户按下“关闭”按钮后,alertView应该消失。 就这样。 无论如何,警报视图不起作用。 如果确实发生异常,则会显示警报视图,但是当我按下关闭按钮时,什么也没有发生,并且程序仍然冻结。

是因为我在@catch块之内使用了alertView还是类似的东西发生了邪恶吗?

提前感谢。

@try内部到底是什么引发异常? 对于错误处理,通常不赞成使用Objective-C中的异常处理。 Objective-C编程语言文档说:

在Objective-C中,异常会占用大量资源。 您不应将异常用于一般的流控制,或仅用于表示错误。 相反,您应该使用方法或函数的返回值来指示已发生错误,并在错误对象中提供有关问题的信息。

异常编程主题”指南具有类似的观点:

重要说明:您应该保留对编程的异常使用或意外的运行时错误,例如越界集合访问,尝试使不可变对象发生突变,发送无效消息以及与窗口服务器的连接断开。 通常,在创建应用程序时(而不是在运行时)会处理这类带有异常的错误。

错误处理编程指南》也很不错。

在主线程上必须进行许多绘图操作。 如果将警报视图代码移至其自己的方法,然后在@catch中调用,则可能会找到答案:

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

...其中myAlertMethod是将警报代码移至的方法。

暂无
暂无

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

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