iOS UIAlertController,uialertcontroller

來源:互聯網
上載者:User

iOS UIAlertController,uialertcontroller

在Xcode的iOS9.0 SDK中,UIAlertView和UIActionSheet都被UIAlertController取代。

在iOS 9中,UIAlertController在功能上是和UIAlertView以及UIActionSheet相同的,UIAlertController以一種模組化替換的方式來代替這兩貨的功能和作用。是使用對話方塊(alert)還是使用上拉菜單(action sheet),就取決於在建立控制器時,您是如何設定首選樣式的。

1、對話方塊

    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"標題"message:@"這個是UIAlertController的預設樣式"preferredStyle:UIAlertControllerStyleAlert];        [self presentViewController:alertController animated:YES completion:nil];

同建立UIAlertView相比,我們無需指定代理,也無需在初始化過程中指定按鈕。不過要特別注意第三個參數,要確定您選擇的是對話方塊樣式還是上拉菜單樣式。

 

2、將動作按鈕添加到控制器

通過建立UIAlertAction的執行個體,您可以將動作按鈕添加到控制器上。UIAlertAction由標題字串、樣式以及當使用者選中該動作時啟動並執行代碼塊組成。通過UIAlertActionStyle,您可以選擇如下三種動作樣式:常規(default)、取消(cancel)以及警示(destruective)。為了實現原來我們在建立UIAlertView時建立的按鈕效果,我們只需建立這兩個動作按鈕並將它們添加到控制器上即可。

   UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"標題"message:@"這個是UIAlertController的預設樣式"preferredStyle:UIAlertControllerStyleAlert];    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"好的" style:UIAlertActionStyleDefault handler:nil];    [alertController addAction:cancelAction];    [alertController addAction:okAction];    [self presentViewController:alertController animated:YES completion:nil];

3、“警示”樣式

    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"標題"message:@"這個是UIAlertController的預設樣式"preferredStyle:UIAlertControllerStyleAlert];    UIAlertAction *resetAction = [UIAlertAction actionWithTitle:@"重設" style:UIAlertActionStyleDestructive handler:nil];    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];        //添加順序和顯示順序相同    [alertController addAction:cancelAction];    [alertController addAction:resetAction];    [self presentViewController:alertController animated:YES completion:nil];

 

4、文本對話方塊

設定輸入框,並且設定通知,檢測輸入字元長度少於3時,不能點擊“好的”。

值得注意的是,不像使用UIAlertView的時候可以用UIAlertViewDelegate檢測文字框輸入,UIAlertController沒有對應的代理,只能自己實現監聽。

    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"文本對話方塊" message:@"登入和密碼對話方塊樣本" preferredStyle:UIAlertControllerStyleAlert];    [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField){        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(alertTextFieldDidChange:) name:UITextFieldTextDidChangeNotification object:textField];        textField.placeholder = @"登入";    }];        [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(alertTextFieldDidChange:) name:UITextFieldTextDidChangeNotification object:textField];        textField.placeholder = @"密碼";        textField.secureTextEntry = YES;    }];            UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"好的" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {        UITextField *login = alertController.textFields.firstObject;        UITextField *password = alertController.textFields.lastObject;        NSLog(@"%@",login.text);        NSLog(@"%@",password.text);    }];        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];    [alertController addAction:cancelAction];    [alertController addAction:okAction];            [self presentViewController:alertController animated:YES completion:nil];

 

- (void)alertTextFieldDidChange:(NSNotification *)notification{    UIAlertController *alertController = (UIAlertController *)self.presentedViewController;    if (alertController) {                UITextField *login = alertController.textFields.firstObject;        UIAlertAction *okAction = alertController.actions.lastObject;        okAction.enabled = login.text.length > 2;    }}

 

5、上拉菜單

    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"儲存或刪除資料" message:@"刪除資料將不可恢複" preferredStyle: UIAlertControllerStyleActionSheet];        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * action) {        NSLog(@"點擊了取消");    }];        UIAlertAction *deleteAction = [UIAlertAction actionWithTitle:@"刪除" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * action) {        NSLog(@"點擊了刪除");    }];    UIAlertAction *archiveAction = [UIAlertAction actionWithTitle:@"儲存" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {        NSLog(@"點擊了儲存");    }];            [alertController addAction:cancelAction];    [alertController addAction:deleteAction];    [alertController addAction:archiveAction];        [self presentViewController:alertController animated:YES completion:nil];

 

 

相關文章

聯繫我們

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