iOS8中提示框的使用UIAlertController(UIAlertView和UIActionSheet變形平板)

來源:互聯網
上載者:User

標籤:

iOS8推出了幾個新的“controller”,主要是把類似之前的UIAlertView變成了UIAlertController,這不經意的改變,貌似把我之前理解的“controller”一下子推翻了~但是也無所謂,有新東西不怕,學會使用了就行。接下來會探討一下這些個新的Controller。

 

- (void)showOkayCancelAlert {    NSString *title = NSLocalizedString(@"A Short Title Is Best", nil);    NSString *message = NSLocalizedString(@"A message should be a short, complete sentence.", nil);    NSString *cancelButtonTitle = NSLocalizedString(@"Cancel", nil);    NSString *otherButtonTitle = NSLocalizedString(@"OK", nil);    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];        // Create the actions.    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:cancelButtonTitle style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {        NSLog(@"The \"Okay/Cancel\" alert‘s cancel action occured.");    }];        UIAlertAction *otherAction = [UIAlertAction actionWithTitle:otherButtonTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {        NSLog(@"The \"Okay/Cancel\" alert‘s other action occured.");    }];        // Add the actions.    [alertController addAction:cancelAction];    [alertController addAction:otherAction];        [self presentViewController:alertController animated:YES completion:nil];}

這是最普通的一個alertcontroller,一個取消按鈕,一個確定按鈕。

新的alertcontroller,其初始化方法也不一樣了,按鈕回應程式法綁定使用了block方式,有利有弊。需要注意的是不要因為block導致了引用迴圈,記得使用__weak,尤其是使用到self。

上面的介面如下:

如果UIAlertAction *otherAction這種otherAction多幾個的話,它會自動排文成如下:

另外,很多時候,我們需要在alertcontroller中添加一個輸入框,例如輸入密碼:

這時候可以添加如下代碼:

 [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {        // 可以在這裡對textfield進行定製,例如改變背景色        textField.backgroundColor = [UIColor orangeColor];    }];

而改變背景色會這樣:

完整的密碼輸入:

- (void)showSecureTextEntryAlert {    NSString *title = NSLocalizedString(@"A Short Title Is Best", nil);    NSString *message = NSLocalizedString(@"A message should be a short, complete sentence.", nil);    NSString *cancelButtonTitle = NSLocalizedString(@"Cancel", nil);    NSString *otherButtonTitle = NSLocalizedString(@"OK", nil);    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];        // Add the text field for the secure text entry.    [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {        // Listen for changes to the text field‘s text so that we can toggle the current        // action‘s enabled property based on whether the user has entered a sufficiently        // secure entry.        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleTextFieldTextDidChangeNotification:) name:UITextFieldTextDidChangeNotification object:textField];        textField.secureTextEntry = YES;    }];    // Create the actions.    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:cancelButtonTitle style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {        NSLog(@"The \"Secure Text Entry\" alert‘s cancel action occured.");        // Stop listening for text changed notifications.        [[NSNotificationCenter defaultCenter] removeObserver:self name:UITextFieldTextDidChangeNotification object:alertController.textFields.firstObject];    }];    UIAlertAction *otherAction = [UIAlertAction actionWithTitle:otherButtonTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {        NSLog(@"The \"Secure Text Entry\" alert‘s other action occured.");        // Stop listening for text changed notifications.        [[NSNotificationCenter defaultCenter] removeObserver:self name:UITextFieldTextDidChangeNotification object:alertController.textFields.firstObject];    }];        // The text field initially has no text in the text field, so we‘ll disable it.    otherAction.enabled = NO;    // Hold onto the secure text alert action to toggle the enabled/disabled state when the text changed.    self.secureTextAlertAction = otherAction;    // Add the actions.    [alertController addAction:cancelAction];    [alertController addAction:otherAction];        [self presentViewController:alertController animated:YES completion:nil];}

注意四點:

1.添加通知,監聽textfield內容的改變:

// Add the text field for the secure text entry.    [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {        // Listen for changes to the text field‘s text so that we can toggle the current        // action‘s enabled property based on whether the user has entered a sufficiently        // secure entry.        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleTextFieldTextDidChangeNotification:) name:UITextFieldTextDidChangeNotification object:textField];        textField.secureTextEntry = YES;    }];

2.初始化時候,禁用“ok”按鈕:

otherAction.enabled = NO;

self.secureTextAlertAction = otherAction;//定義一個全域變數來儲存

3.當輸入超過5個字元時候,使self.secureTextAlertAction = YES:

- (void)handleTextFieldTextDidChangeNotification:(NSNotification *)notification {    UITextField *textField = notification.object;    // Enforce a minimum length of >= 5 characters for secure text alerts.    self.secureTextAlertAction.enabled = textField.text.length >= 5;}

4.在“OK”action中去掉通知:

UIAlertAction *otherAction = [UIAlertAction actionWithTitle:otherButtonTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {        NSLog(@"The \"Secure Text Entry\" alert‘s other action occured.");        // Stop listening for text changed notifications.        [[NSNotificationCenter defaultCenter] removeObserver:self name:UITextFieldTextDidChangeNotification object:alertController.textFields.firstObject];    }];

 

最後是以前經常是alertview與actionsheet結合使用,這裡同樣也有:

- (void)showOkayCancelActionSheet {    NSString *cancelButtonTitle = NSLocalizedString(@"Cancel", nil);    NSString *destructiveButtonTitle = NSLocalizedString(@"OK", nil);        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];        // Create the actions.    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:cancelButtonTitle style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {        NSLog(@"The \"Okay/Cancel\" alert action sheet‘s cancel action occured.");    }];        UIAlertAction *destructiveAction = [UIAlertAction actionWithTitle:destructiveButtonTitle style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {        NSLog(@"The \"Okay/Cancel\" alert action sheet‘s destructive action occured.");    }];        // Add the actions.    [alertController addAction:cancelAction];    [alertController addAction:destructiveAction];        [self presentViewController:alertController animated:YES completion:nil];}

在底部顯示如下:

 

好了,至此,基本就知道這個新的controller到底是怎樣使用了。

iOS8中提示框的使用UIAlertController(UIAlertView和UIActionSheet變形平板)

聯繫我們

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