iOS8中提示框的使用UIAlertController(UIAlertView和UIActionSheet二合一)
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二合一)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: MongoDB【最新版V2.6】- 发行
- 下一篇: x86汇编指令具体解释