2017-02-11 27 views
4

我想在一個Pacman遊戲中使用這段代碼,我從某個網站上得到,但不得不將UIAlertView更改爲UIAlertController,除了下面的代碼有兩個錯誤,我不知道如何解決(我真的新的節目,覺得這是一個非常新手問題 - 對不起!!)如何使用UIAlertController

第一個錯誤是4行:選擇沒有已知的類方法alertControllerWithTitle

第二個錯誤是最後一行:沒有明顯的接口聲明的選擇「show」

謝謝!!!

- (void)collisionWithExit: (UIAlertController *)alert { 

if (CGRectIntersectsRect(self.pacman.frame, self.exit.frame)) { 

    [self.motionManager stopAccelerometerUpdates]; 

    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Congratulations" 
                message:@"You've won the game!" 
                delegate:self 
              cancelButtonTitle:@"OK" 
              otherButtonTitles:nil 
              preferredStyle:UIAlertControllerStyleAlert]; 
    [alert show]; 

    } 

} 
+0

'AlertView'在iOS的9 depricated所以不是必須用'UIAlertController'有更多的選擇。 – vaibhav

+0

[對你有用的鏈接](https://www.google.co.in/?gws_rd=ssl#q=uialertview+deprecated+ios+9+),搜索一下:) – vaibhav

回答

13

請檢查下面的代碼:

UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"My Alert" 
          message:@"This is an alert." 
          preferredStyle:UIAlertControllerStyleAlert]; 

UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault 
           handler:^(UIAlertAction * action) {}]; 

[alert addAction:defaultAction]; 
[self presentViewController:alert animated:YES completion:nil]; 
7

檢查下面這段代碼

UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Name" message:@"YOUR ALERT MESSAGE" preferredStyle:UIAlertControllerStyleAlert]; 

    UIAlertAction *ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) 
             { 
              //BUTTON OK CLICK EVENT 
             }]; 
    UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:nil]; 
    [alert addAction:cancel]; 
    [alert addAction:ok]; 
    [self presentViewController:alert animated:YES completion:nil]; 
相關問題