IOS8 UIAlertController 提示框

來源:互聯網
上載者:User

IOS8 UIAlertController 提示框

IOS8中,Apple將UIActionSheet和UIAlertView整合成一個介面UIAlertController。

原來的是一個view,展示在window視圖之上。現在改成了controller,展示方式變成由當前的controller直接present出來。

 

  1. UIAlertController *alert = [UIAlertController alertControllerWithTitle:@This is Title
  2. message:@This is message
  3. preferredStyle:UIAlertControllerStyleAlert];
  4. [alert addAction:[UIAlertAction actionWithTitle:@Action 1 (Default Style)
  5. style:UIAlertActionStyleDefault
  6. handler:^(UIAlertAction *action) {
  7. NSLog(@Action 1 Handler Called);
  8. }]];
  9. [alert addAction:[UIAlertAction actionWithTitle:@Action 2 (Cancel Style)
  10. style:UIAlertActionStyleCancel
  11. handler:^(UIAlertAction *action) {
  12. NSLog(@Action 2 Handler Called);
  13. }]];
  14. [alert addAction:[UIAlertAction actionWithTitle:@Action 3 (Destructive Style)
  15. style:UIAlertActionStyleDestructive
  16. handler:^(UIAlertAction *action) {
  17. NSLog(@Action 3 Handler Called);
  18. }]];
  19. [self presentViewController:alert animated:YES completion:nil]; 通過addAction介面添加具體按鈕,設定按鈕title、style和使用block方式直接加入按鈕響應介面。

    style有以下幾種:

    [objc]
  20. UIAlertController 提示框> 
  21. UIAlertController 提示框>
    1. typedef NS_ENUM(NSInteger, UIAlertActionStyle) {
    2. UIAlertActionStyleDefault = 0,
    3. UIAlertActionStyleCancel,
    4. UIAlertActionStyleDestructive
    5. } NS_ENUM_AVAILABLE_IOS(8_0);

      與原來的UIActionSheet和UIAlertView比較:

      1、視覺上沒有改變,跟之前的樣式保持一致;

      2、改為controller方式後,控制項的生命週期能夠更好的控制;方便的彈出和收合

      3、當前controller彈出UIAlertController後,再次present controller必須在UIAlertController的基礎上present。present後,UIAlertController會被蓋住。而之前的UIActionSheet和UIAlertView由於是add在window上面的,當在彈出他們的controller基礎上再present controller的話,UIActionSheet和UIAlertView彈框仍然保留在最上方。

      4、修改為controller方式後,介面更加簡單,而且按鈕回應程式式更加明了簡介,不需要在設定delegate和實現響應的回調等。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.