【发布时间】:2016-05-28 10:09:19
【问题描述】:
我想了解如何正确捕获并向用户显示在处理 viewDidLoad 时出现的异常原因?我试图像这样解决这个问题,但我在显示 AlertWindow 时遇到了问题。 错误:由于未捕获的异常,无法识别的选择器发送到实例并终止应用程序。
- (void)viewDidLoad {
@try {
[super viewDidLoad];
APPDataBase *sharedDataBase = [APPDataBase sharedDataBase];
self.navigationItem.hidesBackButton = YES;
recievedArray = [recievedURL componentsSeparatedByString:@" "];
[self fillUpTableViewWithTitles];
if ([self isInternetConnected]){
[sharedDataBase saveData:feeds WithKey:@"savedFeeds"];
}
else
{
feeds = [sharedDataBase loadDataWithKey:@"savedFeeds"];
}
[NSException raise:@"Invalid smth" format:@"Error error error, dangerous, wow"];
}
@catch (NSException *exception) {
[self showAlertWindowWithString:exception];
}
}
和showAlertWindowWithString:方法代码
-(void)showAlertWindowWithString:(NSString *)string{
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:string message:@"Press OK button." preferredStyle:UIAlertControllerStyleAlert];
alertController.view.frame = [[UIScreen mainScreen] applicationFrame];
[alertController addAction:[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){
[self okButtonTapped];
}]];
UIViewController *topRootViewController = [UIApplication sharedApplication].keyWindow.rootViewController;
while (topRootViewController.presentedViewController){
topRootViewController = topRootViewController.presentedViewController;
}
[topRootViewController presentViewController:alertController animated:YES completion:nil];
}
我有点搞砸了,因为我只能访问 iDevice Emulator,所以我想至少使用这种方式来捕捉“现实生活中的设备”错误。或者,也许有其他方法可以让应用在发生此类异常时不崩溃?
【问题讨论】: