2015-10-05 71 views
15

UAlertView已在iOS 9及更高版本中棄用。什麼是替代方案?UIAlertView for iOS 9的替代方案?

UIAlertView *new = [[UIAlertView alloc] initWithTitle:@"Success" message:@"Your InApp Purchases were successfully restored" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
[new show]; 
+1

任何原因,你沒有簡單地看在'UIAlertView'(它告訴你到底該怎麼做)的文檔中,或者在發佈這個問題之前做一個搜索?請在發佈前嘗試找到答案。 – rmaddy

+0

如果您在他們的文檔中找不到答案,請將您的項目更好地轉移到iOS 8.:P –

回答

45

您可以使用此代碼來替換警報視圖:

UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Title" message:@"Message" preferredStyle:UIAlertControllerStyleAlert];   
[alertController addAction:[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil]]; 
[self presentViewController:alertController animated:YES completion:nil]; 

如果需要多個動作,你可以使用:

UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Title" message:@"Message" preferredStyle:UIAlertControllerStyleAlert]; 
[alertController addAction:[UIAlertAction actionWithTitle:@"Button 1" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { 
    // action 1 
}]]; 
[alertController addAction:[UIAlertAction actionWithTitle:@"Button 2" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { 
    // action 2 
}]]; 
[alertController addAction:[UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { 
    [self dismissViewControllerAnimated:YES completion:nil]; 
}]];   
[self presentViewController:alertController animated:YES completion:nil]; 
+1

這需要執行很多代碼......他們在想什麼?雖然沒有委託處理響應的能力是一個很好的補充。 –

+0

@Kundapra Hudga,你爲什麼選擇使用dispatch_async [self presentViewController:alertController animated:YES completion:nil]; ? – Adela

+0

您正在使用UIAlertController,請參閱Swift AlertController教程:https://iosdevcenters.blogspot.com/2016/03/uialertcontroller-in-swift.html –

1
UIAlertController * alert= [UIAlertController 
            alertControllerWithTitle:@"My Title" 
            message:@"Enter User Credentials" 
            preferredStyle:UIAlertControllerStyleAlert]; 

    [self presentViewController:alert animated:YES completion:nil]; 
9

你得到經常的詳細信息,包括通過更換建議-clicking上顯示的類/方法聲明的符號。

UIAlertView情況下,你會看到

「UIAlertView中已被棄用。使用UIAlertController與UIAlertControllerStyleAlert的preferredStyle而不是」

1

我有使用 「UIAlertController」 在iOS 8及更高版本。讓我們看到:

UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Success" message:@"Your InApp Purchases were successfully restored" preferredStyle:UIAlertControllerStyleAlert]; 

並添加按鈕:

UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){ 
      //do something when click button 
}]; 

記住:

[alertController addAction:okAction]; 

然後告訴它:

[self presentViewController:alertController animated:YES completion:nill]; 

如果你想顯示actionsheep,你更改

"preferredStyle:UIAlertControllerStyleActionSheet" 
2
UIAlertController * alert= [UIAlertController 
          alertControllerWithTitle:@"Info" 
          message:@"You are using UIAlertController" 
           preferredStyle:UIAlertControllerStyleAlert]; 

    UIAlertAction* ok = [UIAlertAction 
        actionWithTitle:@"OK" 
        style:UIAlertActionStyleDefault 
        handler:^(UIAlertAction * action) 
        { 
         [alert dismissViewControllerAnimated:YES completion:nil]; 

        }]; 
    UIAlertAction* cancel = [UIAlertAction 
         actionWithTitle:@"Cancel" 
         style:UIAlertActionStyleDefault 
         handler:^(UIAlertAction * action) 
         { 
          [alert dismissViewControllerAnimated:YES completion:nil]; 
         }]; 
[alert addAction:ok]; 
[alert addAction:cancel]; 

[self presentViewController:alert animated:YES completion:nil];