iOS iOS8新特性-UIAlertController

iOS iOS8新特性--UIAlertControlleriphone

1. iOS7及iOS7以前警告類控件有UIAlertView和UIActionSheet

1.1 UIAlertView的使用

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

    alert.alertViewStyle = UIAlertViewStylePlainTextInput;blog

    /**   alertViewStyle的枚舉值以下ip

     UIAlertViewStyleDefault = 0,get

     UIAlertViewStyleSecureTextInput,it

     UIAlertViewStylePlainTextInput,io

     UIAlertViewStyleLoginAndPasswordInputast

     */cli

    [alert show];object

//  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+UIActionSheet

2.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上不一樣的顯示樣式
  

相關文章
相關標籤/搜索