创建及使用UIAlertController
2015-12-30 00:00
281 查看
一。UIAlertControllerStyleActionSheet底部
typedef NS_ENUM(NSInteger, UIAlertControllerStyle) {
UIAlertControllerStyleActionSheet = 0,底部
UIAlertControllerStyleAlert 中间
}
注:在苹果上,Cancel按钮在左边/最底部,其他的按添加顺序显示。
UIAlertActionStyle有三种:UIAlertActionStyleDefault = 0, UIAlertActionStyleCancel,和UIAlertActionStyleDestructive,除了style为cancel只可以addiction一次,其他的可以添加多个按钮。
二。添加文本输入框:UIAlertControllerStyleAlert,中间
1、文本输入框只能添加到Alert的风格中,ActionSheet是不允许的;
2、UIAlertController具有只读属性的textFields数组,需要可直接按自己需要的顺序添加;
3、添加方式是使用block,参数是UITextField;
4、添加UITextField监听方法和实现方法。
- (void)textfieldDidChanged:(NSNotification *)noti {
NSLog(@"\n\n\n%s, noti = %@", __func__, noti);
} 这个是监听方法示例。
三。一个自定义标题颜色文字的实现。
typedef NS_ENUM(NSInteger, UIAlertControllerStyle) {
UIAlertControllerStyleActionSheet = 0,底部
UIAlertControllerStyleAlert 中间
}
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"自定义背景" message:nil preferredStyle:UIAlertControllerStyleActionSheet];//创建对象 [self presentViewController:alertController animated:YES completion:nil];//展示alertController,类似于拍照的UIImagePickerController,是一个试图控制器。 UIAlertAction *cancleAction = [UIAlertAction actionWithTitle:@"取消cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) { NSLog(@"cancel");//点击按钮触发的事件在这里写 }];//创建按钮 UIAlertAction *defaultAction = [UIAlertAction actionWithTitle:@"默认default" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { NSLog(@"默认default"); }]; UIAlertAction *defaultAction1 = [UIAlertAction actionWithTitle:@"默认default1" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { NSLog(@"默认default1"); }]; UIAlertAction *resetAction = [UIAlertAction actionWithTitle:@"重置reset" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) { NSLog(@"reset"); }]; UIAlertAction *resetAction1 = [UIAlertAction actionWithTitle:@"重置reset1" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) { NSLog(@"reset1"); }]; [alertController addAction:cancleAction];//添加按钮 [alertController addAction:defaultAction1]; [alertController addAction:defaultAction]; [alertController addAction:resetAction]; [alertController addAction:resetAction1];
注:在苹果上,Cancel按钮在左边/最底部,其他的按添加顺序显示。
UIAlertActionStyle有三种:UIAlertActionStyleDefault = 0, UIAlertActionStyleCancel,和UIAlertActionStyleDestructive,除了style为cancel只可以addiction一次,其他的可以添加多个按钮。
二。添加文本输入框:UIAlertControllerStyleAlert,中间
1、文本输入框只能添加到Alert的风格中,ActionSheet是不允许的;
2、UIAlertController具有只读属性的textFields数组,需要可直接按自己需要的顺序添加;
3、添加方式是使用block,参数是UITextField;
4、添加UITextField监听方法和实现方法。
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"自定义背景" message:nil preferredStyle:UIAlertControllerStyleAlert];//创建对象 UIAlertAction *cancleAction = [UIAlertAction actionWithTitle:@"取消cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) { NSLog(@"cancel");//点击按钮触发的事件在这里写 }];//创建按钮 [alertController addAction:cancleAction];//添加上去 [alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) { textField.placeholder = @"111111"; }];//添加输入框 [alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) { textField.placeholder = @"34444"; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textfieldDidChanged:) name:UITextFieldTextDidChangeNotification object:textField];//添加UITextFieldTextDidChangeNotification通知,一旦输入框text改变,在textfieldDidChanged:方法里可检测到输入情况 }]; [alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) { textField.placeholder = @"jianting"; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textfieldDidChanged:) name:UITextFieldTextDidChangeNotification object:textField]; }]; UIAlertAction *getAction = [UIAlertAction actionWithTitle:@"默认default" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { UITextField *login = alertController.textFields[0]; UITextField *password = alertController.textFields[1]; UITextField *three = alertController.textFields[2]; [self.view endEditing:YES]; NSLog(@"登陆:%@, 密码:%@,three : %@", login.text, password.text,three.text ); }];//获取输入框中的内容 [alertController addAction:getAction];//添加按钮,点击时触发上面handle的操作。 [self presentViewController:alertController animated:YES completion:nil];//展示视图控制器,这个必须写在后面,写在添加textfield之前显示不出输入框的。
- (void)textfieldDidChanged:(NSNotification *)noti {
NSLog(@"\n\n\n%s, noti = %@", __func__, noti);
} 这个是监听方法示例。
三。一个自定义标题颜色文字的实现。
UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"Dont care what goes here, since we're about to change below" message:@"" preferredStyle:UIAlertControllerStyleActionSheet]; NSMutableAttributedString *hogan = [[NSMutableAttributedString alloc] initWithString:@"Presenting the great... Hulk Hogan!"]; [hogan addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:50.0] range:NSMakeRange(24, 11)]; [alertVC setValue:hogan forKey:@"attributedTitle"]; UIAlertAction *button = [UIAlertAction actionWithTitle:@"Label text" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){ //add code to make something happen once tapped }]; // UIImage *accessoryImage = [UIImage imageNamed:@"someImage"]; // [button setValue:accessoryImage forKey:@"image"]; [alertVC addAction:button]; [self presentViewController:alertVC animated:YES completion:nil];
相关文章推荐
- ListView requestFocus 导致刷新
- Base58 随机UUID
- 关于 OnCloseQuery: 顺序、不能关机等(所有的windows的广播消息都是逐窗口传递的)——如果一个窗体的OnCloseQuery事件中如果写了代码那么WM_QUERYENDSESSION消息就传不过去了msg.result会返回0,关机事件也就停止了
- 【HDOJ】4057 Rescue the Rabbit
- UIScrollView新手教程
- Java 中字符串拼接 String 和 StringBuilder(StringBuffer)的使用
- 如何正确地写好一个界面
- iOS UITableView(五) cell的文字操作及响应事件
- UICollectionView
- 运行rails server 出现There was an error while trying to load the gem 'uglifier'. (Bundler::GemRequireErr
- Android开发中遇到的requestFeature() must be called before adding content异常
- 从头认识java-15.6 队列(Queue)
- 与UI线程通讯
- UIControl类控件(三)
- UI 第一步 UIView
- UIControl类控件(二)
- ugui ui相对位置的计算,以及如何把ui限制在屏幕内
- UIControl类控件(一)
- UI- UIWindow & UIView
- easyui项目小记