只见UIAlertController笑,不见UIAlertView哭

今天跟着视频教学准备在问题回答完成后跳出一个UIAlertView提醒答题结束。敲代码时就发现自动提示上带着一道禁止的红色提示。运行时果然报错了。

从iOS8开始,苹果开始用UIAlertController替代UIAlertView,到了iOS9似乎是彻底不能用了。

在谷歌上搜索了UIAlertController的使用方法,将视频中的代码改写如下:

UIAlertController* alert=[UIAlertControlleralertControllerWithTitle:@"Congratulations" message:[NSStringstringWithFormat:@"CorrectAnswer:%d",self.correctCount] preferredStyle:UIAlertControllerStyleAlert];

UIAlertAction* reset = [UIAlertAction actionWithTitle:@"Reset" style:UIAlertActionStyleDefault handler:^(UIAlertAction* action){

self.index=0;///点击Reset后的动作,回到第一个的猜题界面

intcoin=[self.coinView.currentTitleintValue];

coin=10000;

[self.coinViewsetTitle:[NSStringstringWithFormat:@"%d",coin]forState:UIWindowLevelNormal];

[self viewDidLoad];///网上的例子只要dismiss这个UIAlertController,运行后再点击其他按钮会报错,就改成了viewDidLoad

}];

UIAlertAction* cancel = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleDefault handler:^(UIAlertAction* action){

self.index--;

[self  nextClick];

}];

[alert  addAction:ok];

[alert  addAction:cancel];

[self  presentViewController:alertanimated:YEScompletion:nil];

详细代码已上传全球最大的同性交友网站,可参考github。网上的例子有7道题,删除了一些侵犯肖像权的只剩了两道。。。

运行截图如下


只见UIAlertController笑,不见UIAlertView哭_第1张图片

你可能感兴趣的:(只见UIAlertController笑,不见UIAlertView哭)