您的位置:首页 > 产品设计 > UI/UE

创建及使用UIAlertController

2015-12-30 00:00 281 查看
一。UIAlertControllerStyleActionSheet底部
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];
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: