创建及使用UIAlertController

本文详细介绍了如何使用 UIAlertController 创建自定义的底部弹窗和中间弹窗,包括如何添加按钮、文本输入框以及自定义标题样式等内容。

一。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];






转载于:https://my.oschina.net/u/2560887/blog/591501

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值