2011-02-14 104 views

回答

509

是的,一個UIAlertView可能是你在找什麼。這裏有一個例子:

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"No network connection" 
               message:@"You must be connected to the internet to use this app." 
               delegate:nil 
             cancelButtonTitle:@"OK" 
             otherButtonTitles:nil]; 
[alert show]; 
[alert release]; 

如果你想要做一些更花哨,說你UIAlertView顯示自定義UI,你也可以繼承UIAlertView,放在自定義UI組件的init方法。如果您想在出現UIAlertView後回覆按鈕按鈕,則可以設置上面的delegate並實施- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex方法。您可能還想看看UIActionSheet

+42

Apple文檔中提到「UIAlertView類旨在按原樣使用,不支持子類」。 https://developer.apple.com/library/ios/#documentation/uikit/reference/UIAlertView_Class/UIAlertView/UIAlertView.html – JOM 2012-02-06 05:34:35

+36

只是一個評論:啓用ARC後,'[alert release]'是不需要的(至少,編譯器這樣說)。 – 2012-10-02 10:02:05

+4

UIAlertView的子類化不支持iOS 4以上 – SourabhV 2012-10-29 18:07:36

53

自從iOS 8發佈以來,UIAlertView現在已被棄用。現在您將使用UIAlertController

下面是一個示例中的樣子斯威夫特

let alert = UIAlertController(title: "Hello!", message: "Message", preferredStyle: UIAlertControllerStyle.alert) 
let alertAction = UIAlertAction(title: "OK!", style: UIAlertActionStyle.default) 
{ 
    (UIAlertAction) -> Void in 
} 
alert.addAction(alertAction) 
present(alert, animated: true) 
{ 
    () -> Void in 
} 

正如你所看到的API使我們能夠實現這兩個回調的動作,當我們提出這是非常方便的警惕!

136

不同的人來到這個問題意味着通過彈出框不同的事情。我強烈建議閱讀Temporary Views文檔。我的答案主要是對這個和其他相關文件的總結。

警報(show me an example)

enter image description here

Alerts顯示一個標題和一個可選的消息。在繼續之前,用戶必須確認它(一鍵通知)或做出簡單的選擇(雙鍵警報)。您使用UIAlertController創建警報。

值得引用關於創建不必要的警報的文檔警告和建議。

enter image description here

注:

行動表上(show me an example)

enter image description here

Action Sheets給用戶的選擇列表。它們出現在屏幕的底部或彈出窗口,具體取決於設備的大小和方向。與警報一樣,UIAlertController用於製作操作表。iOS的8之前,UIActionSheet使用,但現在documentation說:

要點:(注意:UIActionSheetDelegate也已過時)UIActionSheet在iOS的8棄用創建和iOS 8的管理動作片和之後,替代地使用UIAlertControllerpreferredStyleUIAlertControllerStyleActionSheet

模式的看法(show me an example)

enter image description here

一個modal view是具有它需要完成任務的一切自包含的視圖。它可能會或可能不會佔用整個屏幕。要創建模態視圖,請將UIPresentationControllerModal Presentation Styles之一一起使用。

參見

酥料餅(show me an example)

enter image description here

Popover是一個視圖當用戶點擊某物並在點擊時消失時出現。它有一個箭頭顯示了水龍頭的製造位置。內容可以是任何可以放入View Controller的內容。你用UIPopoverPresentationController做一個popover。 (iOS版8之前,UIPopoverController是推薦的方法。)

在過去popovers只在iPad上使用,但與iOS 8日起,你也可以讓他們在iPhone上(見hereherehere)。

又見

通知

enter image description here

Notifications的聲音/振動,警報/橫幅,或徽標,通知的東西,用戶即使在應用程序未在前臺運行。

enter image description here

又見

關於Android祝酒詞的說明

enter image description here

在Android中,Toast是一條短消息,在屏幕上顯示一小段時間,然後自動消失,而不會中斷用戶與應用的交互。

來自Android背景的人們想知道什麼是Toast的iOS版本。這些問題的一些例子可以發現here,here,herehere。答案是在iOS中沒有相當於Toast的東西。已經提出的各種解決辦法包括:

  • 使自己有一個子類UIView
  • 導入模仿舉杯
  • 使用釦子警報帶有計時器的第三方項目

然而,我的建議是堅持使用iOS已有的標準UI選項。不要試圖讓您的應用的外觀和行爲與Android版本完全相同。想想如何重新包裝它,使它看起來像一個iOS應用程序。

22

更新的iOS 8.0

由於iOS的8.0,你將需要使用UIAlertController如下所示:

-(void)alertMessage:(NSString*)message 
{ 
    UIAlertController* alert = [UIAlertController 
      alertControllerWithTitle:@"Alert" 
      message:message 
      preferredStyle:UIAlertControllerStyleAlert]; 

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

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

凡自我在我的例子是一個UIViewController,它實現了 「presentViewController」 方法爲一個彈出。

大衛

8

對於斯威夫特3 &斯威夫特4:

由於UIAlertView中已被棄用,有一個顯示報警的好辦法上斯威夫特3

let alertController = UIAlertController(title: NSLocalizedString("No network connection",comment:""), message: NSLocalizedString("connected to the internet to use this app.",comment:""), preferredStyle: .alert) 
let defaultAction = UIAlertAction(title:  NSLocalizedString("Ok", comment: ""), style: .default, handler: { (pAlert) in 
       //Do whatever you wants here 
     }) 
alertController.addAction(defaultAction) 
self.present(alertController, animated: true, completion: nil) 

推薦使用:

這是迅捷版本通過檢查響應啓發:

顯示AlertView:

let alert = UIAlertView(title: "No network connection", 
          message: "You must be connected to the internet to use this app.", delegate: nil, cancelButtonTitle: "Ok") 
    alert.delegate = self 
    alert.show() 

委託添加到您的視圖控制器:

class AgendaViewController: UIViewController, UIAlertViewDelegate 

當按鈕用戶點擊,該代碼就會被執行:

func alertView(alertView: UIAlertView, clickedButtonAtIndex buttonIndex: Int) { 


} 
0

這裏是Xamarin.iOS的C#版本

var alert = new UIAlertView("Title - Hey!", "Message - Hello iOS!", null, "Ok"); 
alert.Show(); 
4

雖然我已經寫了一個overview不同類型的彈出窗口,但大多數人只需要一個Alert。

如何實現一個彈出式對話框

enter image description here

class ViewController: UIViewController { 

    @IBAction func showAlertButtonTapped(_ sender: UIButton) { 

     // create the alert 
     let alert = UIAlertController(title: "My Title", message: "This is my message.", preferredStyle: UIAlertControllerStyle.alert) 

     // add an action (button) 
     alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil)) 

     // show the alert 
     self.present(alert, animated: true, completion: nil) 
    } 
} 

我更全面的回答是here