提示框控制器:UIAlertController
typedef NS_ENUM(NSInteger, UIAlertControllerStyle) {
UIAlertControllerStyleActionSheet = 0, //在視圖底部彈出的提示框,它不能添加文本框,而且在ipad中必須使用popover形式展示
UIAlertControllerStyleAlert //在視圖中間彈出的提示框
} NS_ENUM_AVAILABLE_IOS(8_0);
typedef NS_ENUM(NSInteger, UIAlertActionStyle) {
UIAlertActionStyleDefault = 0, //默認的確認按鈕
UIAlertActionStyleCancel, //默認的取消按鈕
UIAlertActionStyleDestructive //默認的紅色按鈕
}NS_ENUM_AVAILABLE_IOS(8_0);
3、UIAlertController:提示框控制器類
@interface UIAlertController : UIViewController
方法:
//創建提示框控制器的類方法
+ (instancetype)alertControllerWithTitle:(NSString *)title message:(NSString *)message preferredStyle:(UIAlertControllerStyle)preferredStyle;
//在提示框上添加文本框的實例方法(只能在UIAlertView風格的提示框添加)
- (void)addTextFieldWithConfigurationHandler:(void (^)(UITextField *textField))configurationHandler;
//在提示框上添加按鈕
- (void)addAction:(UIAlertAction *)action;
屬性:
//提示框上存放所有按鈕的數組
@property (nonatomic, readonly) NSArray *actions;
//提示框上存放所有文本框的數組
@property (nonatomic, readonly) NSArray *textFields;
//提示框的標題
@property (nonatomic, copy) NSString *title;
//提示信息
@property (nonatomic, copy) NSString *message;
//提示框控制器的風格
@property (nonatomic, readonly) UIAlertControllerStyle preferredStyle;
@end
4、UIAlertAction:提示框按鈕
@interface UIAlertAction : NSObject <NSCopying>
方法:
//創建提示框按鈕的類方法
+ (instancetype)actionWithTitle:(NSString *)title style:(UIAlertActionStyle)style handler:(void (^)(UIAlertAction *action))handler;
屬性:
//按鈕標題
@property (nonatomic, readonly) NSString *title;
//按鈕的風格
@property (nonatomic, readonly) UIAlertActionStyle style;
//按鈕是否有效
@property (nonatomic, getter=isEnabled) BOOL enabled;
@end
具體的實例如下:
創建步驟:
1、布局故事板,在控制器的視圖中拖入一個按鈕,並關聯IBAction事件
2、在按鈕的關聯事件中的主要代碼如下:
//創建提示框控制器
//創建提示框控制器 UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示框" message:@"消息" preferredStyle:UIAlertControllerStyleAlert]; alertController.view.backgroundColor = [UIColor purpleColor];
//創建提示框按鈕
//創建提示按鈕 UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"默認Cancel" style:UIAlertActionStyleCancel handler:nil]; UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"默認Default" style:UIAlertActionStyleDefault handler:nil]; UIAlertAction *action3 = [UIAlertAction actionWithTitle:@"默認Destructive" style:UIAlertActionStyleDestructive handler:nil];
//添加提示按鈕到提示框中
//添加提示按鈕 [alertController addAction:action1]; [alertController addAction:action2]; [alertController addAction:action3];
//添加文本框到提示框中(只適合提示框風格為:UIAlertControllerStyleAlert)
//添加文本框(只適合alertview類型的提示框) [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) { textField.placeholder = @"賬號"; }]; [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) { textField.placeholder = @"密碼"; textField.secureTextEntry = YES; //安全輸入模式 }];
//給文本框添加監聽事件
//給文本框添加監聽事件(文本框的開始、結束、狀態改變等) [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) { textField.placeholder = @"添加監聽事件"; [textField addTarget:self action:@selector(alertTextFiledDidChanged:) forControlEvents:UIControlEventEditingChanged]; }];
//以模態窗口的形式顯示提示框
[self presentViewController:alertController animated:YES completion:nil];
//實現文本框事件
#pragma mark 文本框監聽事件 -(void)alertTextFiledDidChanged:(NSNotification *)notification { NSLog(@"Enditing changed"); }
//點擊按鈕,顯示演示結果
當沒有添加action3按鈕到提示框,即按鈕個數<=2時,兩種提示框的樣式截圖為:
UIAlertControllerStyleAlert:從屏幕中間彈出
UIAlertControllerStyleActionSheet:從屏幕底部彈出(不能添加文本框)
當沒有添加action3按鈕到提示框,即按鈕個數>=3時,兩種提示框的樣式截圖為:
UIAlertControllerStyleAlert:從屏幕中間彈出
UIAlertControllerStyleActionSheet:從屏幕底部彈出(不能添加文本框)