iOS iOS8新特性--UIAlertController

來源:互聯網
上載者:User

標籤:

1. iOS7及iOS7之前警告類控制項有UIAlertView和UIActionSheet1.1 UIAlertView的使用

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"警告" message:@"這是一個UIAlertView" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:@"關閉", nil];

    alert.alertViewStyle = UIAlertViewStylePlainTextInput;

    /**   alertViewStyle的枚舉值如下

     UIAlertViewStyleDefault = 0,

     UIAlertViewStyleSecureTextInput,

     UIAlertViewStylePlainTextInput,

     UIAlertViewStyleLoginAndPasswordInput

     */

    [alert show];

//  UIAlertViewDelegate

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex;

1.2 UIActionSheet的使用

    UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@"這是一個UIActionSheet" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"確定"

                                              otherButtonTitles:@"關閉", nil];

    [sheet showInView:self.view];

// UIActionSheetDelegate

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex

2. iOS8開始,用UIAlertController替代UIAlertView+UIActionSheet2.1 UIAlertController的使用 (Style: UIAlertControllerStyleAlert)

    // 初始化UIAlertController

  UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"警告" message:@"iOS新控制項UIAlertController" preferredStyle:UIAlertControllerStyleAlert];

    // 添加按鈕,注意如果Block的循環參考

    __weak typeof(alert) weakAlert = alert;

    [alert addAction:[UIAlertAction actionWithTitle:@"確定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {

    // block內部寫點擊按鈕之後做的事情

    NSLog(@"點擊了確定按鈕--%@-%@", [weakAlert.textFields.firstObject text], [weakAlert.textFields.lastObject text]);

    }]];

    // 添加文字框

    [alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {

        textField.textColor = [UIColor blueColor];

        textField.text = @"請輸入使用者名稱";

        [textField addTarget:self action:@selector(usernameDidChange:) forControlEvents:UIControlEventEditingChanged];

         // 也可以用通知監聽TextField的變化

     //[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(usernameDidChange:) name:UITextFieldTextDidChangeNotification object:textField];

    }];

    [alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {

        textField.secureTextEntry = YES;

        textField.text = @"123456";

    }];

    // 彈出UIAlertController

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

 

// textField EditingChanged時,回調的方法

- (void)usernameDidChange:(UITextField *)username

{

    NSLog(@"%@", username.text);

}

 

2.2 UIAlertController的使用 (Style: UIAlertControllerStyleActionSheet)

    // 在2.1代碼上,修改 初始化UIAlertController 的代碼

  UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"警告" message:@"iOS新控制項UIAlertController" preferredStyle:

UIAlertControllerStyleActionSheet];

 

運行程式後報錯,因為text field只能用在Style:UIAlertControllerStyleAlert 的UIAlertController上,把添加textFiled的代碼登出掉,再運行程式就正常了

reason: ‘Text fields can only be added to an alert controller of style UIAlertControllerStyleAlert‘

 

2.3通過上面的介紹,可以看出蘋果有意把UIAlertView,UIActionSheet統一在一起,實際上,蘋果還統一了iphone和ipad的UIAlertController的處理方式

2.3.1 建立項目時,Devices選擇Universal,語言選OC(錯誤)

2.3.2 在Main.storyboard中拖導航控制器,讓mainViewController成為導航控制器的rootViewController

2.3.3 程式碼範例

  // 初始化UIAlertController

    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"警告" message:@"iOS新控制項UIAlertController" preferredStyle:

UIAlertControllerStyleAlert];

    // 設定popover指向的item

    alert.popoverPresentationController.barButtonItem = self.navigationItem.leftBarButtonItem;

    // 添加按鈕

    [alert addAction:[UIAlertAction actionWithTitle:@"確定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {

        NSLog(@"點擊了確定按鈕");

    }]];

    [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {

        NSLog(@"點擊了取消按鈕");

    }]];

    // 彈出UIAlertController

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

注:左圖是在iphone上運行效果,右圖是在ipad上的運行效果,Style為UIAlertControllerStyleAlert時,顯示樣式一致

  

 

2.3.4 把2.3.3的初始化代碼改成Style:UIAlertControllerStyleActionSheet

    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"警告" message:@"iOS新控制項UIAlertController" preferredStyle:UIAlertControllerStyleActionSheet];

 注:左圖是iphone,右圖是ipad,用一份代碼實現了在iphone和ipad上不同的顯示樣式
  

 

iOS iOS8新特性--UIAlertController

聯繫我們

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