今天跟著視頻教學準備在問題回答完成后跳出一個UIAlertView提醒答題結(jié)束。敲代碼時就發(fā)現(xiàn)自動提示上帶著一道禁止的紅色提示。運行時果然報錯了。
從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];///網(wǎng)上的例子只要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];
詳細代碼已上傳全球最大的同性交友網(wǎng)站,可參考github。網(wǎng)上的例子有7道題,刪除了一些侵犯肖像權(quán)的只剩了兩道。。。
運行截圖如下
