UIAlertViewController是蘋果自帶的信息提示框,僅在iOS8.0以后可以使用
NS_CLASS_AVAILABLE_IOS(8_0) @interface UIAlertController : UIViewController
下面是一個使用的簡單例子:
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:message preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]; UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"好的" style:UIAlertActionStyleDestructive handler:nil]; [alertController addAction:cancelAction]; [alertController addAction:okAction]; [self presentViewController:alertController animated:YES completion:nil];
UIAlertController的alertControllerWithTitle:message:preferredStyle:方法中有個樣式參數:preferredStyle
此參數為枚舉類型:
typedef NS_ENUM(NSInteger, UIAlertControllerStyle) { UIAlertControllerStyleActionSheet = 0, UIAlertControllerStyleAlert } NS_ENUM_AVAILABLE_IOS(8_0);
UIAlertControllerStyleAlert:該樣式與UIAlertView的樣式一樣,顯示在屏幕中央。
UIAlertControllerStyleActionSheet:該樣式顯示在屏幕底部,自下而上推出。
UIAlertAction的actionWithTitle:style:handler:方法中也有一個樣式參數叫做:style
此參數為枚舉類型:
typedef NS_ENUM(NSInteger, UIAlertActionStyle) { UIAlertActionStyleDefault = 0, UIAlertActionStyleCancel, UIAlertActionStyleDestructive } NS_ENUM_AVAILABLE_IOS(8_0);
UIAlertActionStyleDefault是默認樣式,白底黑字,響應touchUpInside事件。
UIAlertActionStyleCancel是取消樣式,與UIAlertActionStyleDefault唯一的不同就是它的字體加粗了。
UIAlertActionStyleDestructive是具有警示性的樣式,與UIAlertActionStyleDefault唯一的不同就是它的字體變為紅色了。
有個問題:UIAlertController只能顯示提示信息嗎?如果是的話,為什么不繼續使用UIAlertView?如果不是,那它還可以做什么?
答:不是。UIAlertController還可以添加文本輸入框,與用戶進行文本信息輸入交互,而不再僅僅是輸出給用戶提示信息。接下來看一下一個簡單的例子:
首先,將alertController聲明為全局對象便於在整個類中使用
@interface ViewController () @property (nonatomic, strong) UIAlertController *alertController; @end
接下來是實例化alertController
self.alertController = [UIAlertController alertControllerWithTitle:@"提示" message:str preferredStyle:UIAlertControllerStyleAlert]; [self.alertController addTextFieldWithConfigurationHandler:^(UITextField *textField){ textField.placeholder = @"登錄"; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(alertViewTextFieldDidChange:) name:UITextFieldTextDidChangeNotification object:textField]; }]; [self.alertController addTextFieldWithConfigurationHandler:^(UITextField *textField){ textField.placeholder = @"密碼"; textField.secureTextEntry = YES; }]; UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]; UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"確定" style:UIAlertActionStyleDestructive handler:nil]; [okAction setEnabled:NO]; [self.alertController addAction:cancelAction]; [self.alertController addAction:okAction]; [self presentViewController:self.alertController animated:YES completion:nil];
較之前面的代碼,這里多了兩行代碼:
[self.alertController addTextFieldWithConfigurationHandler:^(UITextField *textField){ textField.placeholder = @"登錄"; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(alertViewTextFieldDidChange:) name:UITextFieldTextDidChangeNotification object:textField]; }]; [self.alertController addTextFieldWithConfigurationHandler:^(UITextField *textField){ textField.placeholder = @"密碼"; textField.secureTextEntry = YES; }];
這兩行代碼就是在alertController中加入文本輸入框,讓用戶輸入必要信息,以便交互。其中給textField設置了placeholder和secureTextEntry,另外還添加了屬性監聽。
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(alertViewTextFieldDidChange:) name:UITextFieldTextDidChangeNotification object:textField];
此處的監聽事件會在文本框變化時觸發,方法如下:
-(void)alertViewTextFieldDidChange:(NSNotification *)notification { UITextField *textField = notification.object; UIAlertAction *action = self.alertController.actions[1]; [action setEnabled:NO]; if(textField.text.length>3) { NSLog(@"textField.text = %@",textField.text); [action setEnabled:YES]; } }
方法中通過:notification.object來獲取到輸入框,用於后續操作。