NS_CLASS_AVAILABLE_IOS(8_0) @interface UIAlertAction : NSObject <NSCopying> //创建操作
+ (instancetype)actionWithTitle:(nullable NSString *)title style:(UIAlertActionStyle)style handler:(void (^ __nullable)(UIAlertAction *action))handler; NS_CLASS_AVAILABLE_IOS(8_0) @interface UIAlertController : UIViewController //初始化
+ (instancetype)alertControllerWithTitle:(nullable NSString *)title message:(nullable NSString *)message preferredStyle:(UIAlertControllerStyle)preferredStyle; //添加操作
- (void)addAction:(UIAlertAction *)action; 示例1:最简单的提醒视图 //创建操作
UIAlertAction *okAlert = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){
//具体操作内容
}];
//初始化
UIAlertController *alert = [UIAlertControlleralertControllerWithTitle:@"顶部标题栏"message:@"中间信息"preferredStyle:UIAlertControllerStyleAlert];
//添加操作
[alert addAction:okAlert];
//以model形式,显示警告视图
[self presentViewController:alert animated:YES completion:nil]; // 等同于
NSString *title = @"顶部标题栏";
NSString *message = @"中间信息";
NSString *okAlertButton = @"OK";
UIAlertAction *okAlert = [UIAlertAction
actionWithTitle:okAlertButton style:UIAlertActionStyleDefaulthandler:^(UIAlertAction * _Nonnull action) { }]; UIAlertController *alert = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert]; 示例2:多个按键的提醒视图 //取消按键——下次下次 UIAlertActionStyleCancel (粗体)
[alert addAction:[UIAlertAction actionWithTitle:@"下次下次" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"点击取消按钮");
}]]; //红色按键——残忍拒绝 UIAlertActionStyleDestructive
[alert addAction:[UIAlertAction actionWithTitle:@"残忍拒绝" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"红色按钮");
}]]; // 两个会是这样 // 3个就挨个往下排
//普通按键——立马好评 UIAlertActionStyleDefault [alert addAction:[UIAlertAction actionWithTitle:@"立马好评" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"普通按钮");
}]]; 示例3:带对话框TextField - (void)addTextFieldWithConfigurationHandler:(void (^ __nullable)(UITextField *textField))configurationHandler; //添加TextField 条栏 addTextFieldWithConfigurationHandler
[alert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
NSLog(@"TextField");
//中间的提示输入
textField.placeholder = @"用来提示你做什么事的";
}]; 示例4:提醒图标,在最底部呈现 Sheet // UIAlertControllerStyleAlert改成UIAlertControllerStyleActionSheet (最后面的)
UIAlertController *alert = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleActionSheet]; UIAlertControllerStyleActionsheet好像不能和TextField在一起,会报错。是这样吗? 疑问:排列没有次序?
用英文@“ok” 之类的,就会排列和我写的顺序一样。
NS_CLASS_AVAILABLE_IOS(8_0) @interface UIAlertAction : NSObject <NSCopying>
//创建操作
+ (
instancetype)actionWithTitle:(nullable NSString *)title style:(UIAlertActionStyle)style handler:(void (^ __nullable)(UIAlertAction *action))handler;
NS_CLASS_AVAILABLE_IOS(8_0) @interface UIAlertController : UIViewController
//初始化
+ (instancetype)alertControllerWithTitle:(nullable NSString *)title message:(nullable NSString *)message preferredStyle:(UIAlertControllerStyle)preferredStyle;
//添加操作
- (void)addAction:(UIAlertAction *)action;
示例1:最简单的提醒视图
//创建操作
UIAlertAction *okAlert = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){
//具体操作内容
}];
//初始化
UIAlertController *alert = [UIAlertControlleralertControllerWithTitle:@"顶部标题栏"message:@"中间信息"preferredStyle:UIAlertControllerStyleAlert];
//添加操作
[alert addAction:okAlert];
//以model形式,显示警告视图
[self presentViewController:alert animated:YES completion:nil];
// 等同于
NSString *title = @"顶部标题栏";
NSString *message = @"中间信息";
NSString *message = @"中间信息";
NSString *okAlertButton = @"OK";
UIAlertAction *okAlert = [UIAlertAction
actionWithTitle:okAlertButton style:UIAlertActionStyleDefaulthandler:^(UIAlertAction * _Nonnull action) {
}];
UIAlertController *alert = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
示例2:多个按键的提醒视图
//取消按键——下次下次 UIAlertActionStyleCancel (粗体)
[alert addAction:[UIAlertAction actionWithTitle:@"下次下次" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"点击取消按钮");
}]];
//红色按键——残忍拒绝 UIAlertActionStyleDestructive
[alert addAction:[UIAlertAction actionWithTitle:@"残忍拒绝" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"红色按钮");
NSLog(@"红色按钮");
}]];
// 两个会是这样
// 3个就挨个往下排
//普通按键——立马好评 UIAlertActionStyleDefault
[alert addAction:[UIAlertAction actionWithTitle:@"立马好评" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"普通按钮");
NSLog(@"普通按钮");
}]];
示例3:带对话框TextField
- (void)addTextFieldWithConfigurationHandler:(void (^ __nullable)(UITextField *textField))configurationHandler;
//添加TextField 条栏 addTextFieldWithConfigurationHandler
[alert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
NSLog(@"TextField");
//中间的提示输入
textField.placeholder = @"用来提示你做什么事的";
}];
}];
示例4:提醒图标,在最底部呈现 Sheet
// UIAlertControllerStyleAlert改成UIAlertControllerStyleActionSheet (最后面的)
UIAlertController *alert = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertControllerStyleActionsheet好像不能和TextField在一起,会报错。是这样吗?
疑问:排列没有次序?
用英文@“ok” 之类的,就会排列和我写的顺序一样。