UIAlertController
来源:互联网 发布:腾讯q币充值软件 编辑:程序博客网 时间:2024/05/22 10:40
UIAlertController
iOS2.0之后,使用UIAlertView来设置提示框,使用initWithTitle:message: delegate:self cancelButtonTitle:otherButtonTitles:初始化alertView,然后使用-show显示alert。如果想获取button则需要使用delegate方法。(UIActionSheet类似)
在IOS8之后,UIAlertController替代了UIActionSheet和UIAlertView。是UIViewController的子类,它使用工厂方法,利用preferredStyle属性,暂时提供alert以及actionSheet(实际使用时,使用alertControllerWithTitle:message:preferredStyle中preferredStyle即可制定提示框的类型)。
注意,
(1) 这个class不能通过继承的方式来自定义。
(2)UIAlertController必须等到View Controller加载后才能显示,如果将UIAlertController类写在View Controller中没有效果。
1、下面是常用的属性和方法
//在actionWithTitle:Style:handler中的style参数,设置按钮的styletypedef NS_ENUM(NSInteger, UIAlertActionStyle) { UIAlertActionStyleDefault = 0, UIAlertActionStyleCancel,//取消 UIAlertActionStyleDestructive//销毁,红色的字体} NS_ENUM_AVAILABLE_IOS(8_0);//alertControllerWithTitle:message:preferredStyle:初始化UIAlertController时,perferredStyle的参数,表示信息框是ActionSheet还是Alert。typedef NS_ENUM(NSInteger, UIAlertControllerStyle) { UIAlertControllerStyleActionSheet = 0, UIAlertControllerStyleAlert} NS_ENUM_AVAILABLE_IOS(8_0);@interface UIAlertAction : NSObject <NSCopying>+(instancetype)actionWithTitle:(nullable NSString *)title style:(UIAlertActionStyle)style handler:(void (^ __nullable)(UIAlertAction *action))handler;@property (nullable, nonatomic, readonly) NSString *title;@property (nonatomic, readonly) UIAlertActionStyle style;@property (nonatomic, getter=isEnabled) BOOL enabled;@end@interface UIAlertController : UIViewController+(instancetype)alertControllerWithTitle:(nullable NSString *)title message:(nullable NSString *)message preferredStyle:(UIAlertControllerStyle)preferredStyle;//添加向controller中添加action- (void)addAction:(UIAlertAction *)action;@property (nonatomic, readonly) NSArray<UIAlertAction *> *actions;@property (nonatomic, strong, nullable) UIAlertAction *preferredAction NS_AVAILABLE_IOS(9_0);//添加文本框- (void)addTextFieldWithConfigurationHandler:(void (^ __nullable)(UITextField *textField))configurationHandler;@property (nullable, nonatomic, readonly) NSArray<UITextField *> *textFields;//获取所有的文本框@property (nullable, nonatomic, copy) NSString *title;@property (nullable, nonatomic, copy) NSString *message;@property (nonatomic, readonly) UIAlertControllerStyle preferredStyle;@end
2、例子:使用警示框输入账户、密码,实现登录。
-(void)AlertAchieveLogin{ //1、初始化UIAlertController /* +alertControllerWithTitle:message:preferredStyle:声明 title:UIAlertController的标题 message:title下面的解释文字 preferredStyle:AlertController是Alert还是ActionSheet */ UIAlertController *controller = [UIAlertController alertControllerWithTitle:@"登录" message:@"请输入姓名和密码" preferredStyle:UIAlertControllerStyleAlert]; //2、UIAlertAction设置按钮 //取消按钮,alert信息框消失 UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) { [self dismissViewControllerAnimated:YES completion:nil]; }]; //登录按钮 /* +actionWithTitle:style:handler: title:action的标题 style:alertAction的风格,UIAlertActionStyleDefault,UIAlertActionStyleCancel,UIAlertActionStyleDestructive handler:处理用户按下该按钮后要做的操作 */ UIAlertAction *loginAction = [UIAlertAction actionWithTitle:@"登录" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { //点击登录按钮后,获取账户和密码 NSString *account = [controller.textFields objectAtIndex:0].text; NSString *pwd = [controller.textFields objectAtIndex:1].text; NSLog(@"账户是:%@,密码:%@",account,pwd); }]; //3、向UIAlertController添加按钮 [controller addAction:cancelAction]; [controller addAction:loginAction]; //4、设置文本框 [controller addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) { //在文本框中,显示account textField.placeholder = @"account"; textField.layer.masksToBounds = YES; textField.layer.cornerRadius = 10; textField.backgroundColor = [UIColor lightGrayColor]; }]; [controller addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) { //在文本框中,显示password textField.placeholder = @"password"; textField.layer.masksToBounds = YES; textField.layer.cornerRadius = 10; }]; //5、展示信息框 //使用presentViewController:animated:completion:呈现alertController [self presentViewController:controller animated:YES completion:nil];}
阅读全文
0 0
- UIAlertController
- UIAlertController
- UIAlertController
- UIAlertController
- UIAlertController
- UIalertController
- UIAlertController
- UIalertController
- UIAlertController
- UIAlertController
- UIAlertController
- UIAlertController
- UIAlertController
- UIAlertController
- UIAlertController
- UIAlertController
- UIAlertController
- UIAlertController
- java+mysql递归拼接树形JSON列表
- bootstrapTable详解(排序,格式化时间,悬浮)
- 标准JSON格式定义与解析注意点
- java基础复习
- HTML之标签基础汇总
- UIAlertController
- 文章标题
- root
- Fragment
- 神经网络入门二
- 开源大数据周刊-第54期
- 解决Vim使用ctrlp+ag搜索不能扫描隐藏的文件和目录和自定义忽略文件问题
- opencv cvEqualizeHist cvSmooth高斯、均值、中值滤波
- USB描述符解析和USB_CCID描述符设置