iOS 8及以後版本 如何建立UIAlertView?

來源:互聯網
上載者:User

標籤:

1.

Important: UIAlertView is deprecated in iOS 8. (Note that UIAlertViewDelegate is also deprecated.) To create and manage alerts in iOS 8 and later, instead use UIAlertController with a preferredStyle of UIAlertControllerStyleAlert.  

//UIAlertView和UIAlertViewDelegate(代理被給用block回調,更簡單)在iOS8及以後版本中被棄用,改用風格為UIAlertControllerStyleAlert的UIAlertController來替代。

2.

In apps that run in versions of iOS prior to iOS 8, use the UIAlertView class to display an alert message to the user. An alert view functions similar to but differs in appearance from an action sheet (an instance of UIActionSheet).

//iOS8以前版本中UIAlertView和UIActionSheet有著類似的功能,卻通過不同的類來產生。言外之意,iOS8以後版本,UIAlertView和UIActionSheet兩種alert頁面都將通過UIAlertController來產生。

3.

 iOS 8以前版本  如何建立UIAlertView ? 

OBJECTIVE-C

- (instancetype)initWithTitle:(NSString *)title
                      message:(NSString *)message
                     delegate:(id)delegate
            cancelButtonTitle:(NSString *)cancelButtonTitle
            otherButtonTitles:(NSString *)otherButtonTitles,
...

4.

 iOS 8及以後版本  如何建立UIAlertView ?

  1. UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"My Alert"
  2. message:@"This is an alert.” preferredStyle:UIAlertControllerStyleAlert];
  3. UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK”style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {}];
  4. [alert addAction:defaultAction];
  5. [self presentViewController:alert animated:YES completion:nil];

5.

 iOS 8以前版本  如何建立UIActionSheet ?

- (instancetype)initWithTitle:(NSString *)title

                     delegate:(id<UIActionSheetDelegate>)delegate

            cancelButtonTitle:(NSString *)cancelButtonTitle

       destructiveButtonTitle:(NSString *)destructiveButtonTitle

            otherButtonTitles:(NSString *)otherButtonTitles

...

6.

iOS 8及以後版本  如何建立UIActionSheet ?

    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"蒼老師你好" message:@"聽說你的新片被下載了9999次" preferredStyle:UIAlertControllerStyleAlert];

    

    UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"確定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

        self.lblTarget.text = [NSString stringWithFormat:@"點擊AlertView確定按鈕後,產生隨機數"];

        self.lblTarget.textColor = [UIColor redColor];

    }]; //點擊按鈕後通過block回調執行此方法,故沒必要再使用代理了

    

    UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {

    }]; /*UIAlertActionStyleCancel 藍色字型,加粗;

         UIAlertActionStyleDefault 字型藍色,不加粗;

         UIAlertActionStyleDestructive字型紅色,不加粗;

         */

    

    [alertVC addAction:action1];

    [alertVC addAction:action2];

    

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

 

 

 

 

iOS 8及以後版本 如何建立UIAlertView?

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.