iOS 8 中UIAlertController使用
在iOS8中,UIAlertView以及UIActionSheet两个控件被集合到
UIAlertController中,以下记载一下使用方法:
使用UIAlertController演示,自定义时间日期选择器在上拉菜单中:
- (IBAction)selectDateTime:(UIButton *)sender
{
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"这里是标题" message:@"这里是具体信息!\n\n\n\n\n\n\n\n\n\n" preferredStyle:UIAlertControllerStyleActionSheet]
UIDatePicker *datePicker = [[UIDatePicker alloc] init]
datePicker.datePickerMode = UIDatePickerModeDateAndTime
// datePicker.frame = CGRectMake(0, 0, 200, 200)
// CGRect rect = datePicker.frame
// rect.origin = CGPointMake(0, 40)
// datePicker.frame = rect
[alertController.view addSubview:datePicker]
//使用第三方库#import "Masonry.h" 手动添加约束
[datePicker mas_makeConstraints:^(MASConstraintMaker *make)
{
//设置时间选择器居中于alertController
make.centerX.equalTo(alertController.view.mas_centerX)
//时间选择器宽度不要超过alertController
make.width.equalTo(alertController.view).with.offset(-2)
//时间选择弃顶部距离alertController顶部40距离(用于显示“title”和“message”)
make.top.equalTo(alertController.view).with.offset(40)
}]
//点击“确定”按钮后执行,handler代码块
UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//获取选中的日期
NSDate *selectedDate = datePicker.date
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]
dateFormatter.dateFormat = @"YYYY-MM-dd HH:mm"
NSString *strSelectedDate = [dateFormatter stringFromDate:selectedDate]
_labText.text = strSelectedDate
}]
//取消按钮
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消 " style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
_labText.text = @"点击了--取消按钮"
}]
//破坏性按钮
UIAlertAction *destructiveAction = [UIAlertAction actionWithTitle:@"破坏性按钮" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
_labText.text = @"点击了--破坏性按钮"
}]
[alertController addAction:sureAction]
[alertController addAction:cancelAction]
[alertController addAction:destructiveAction]
[self presentViewController:alertController animated:YES completion:^{
self.view.backgroundColor = [UIColor greenColor]
}]
}
使用UIAlertController演示提示框,可以用作登录操作:
- (IBAction)showAlertView:(UIButton *)sender
{
UIAlertController *alertViewController = [UIAlertController alertControllerWithTitle:@"这是Alert视图" message:@"这里是Alert的Message!这里是Alert的Message!这里是Alert的Message!这里是Alert的Message!这里是Alert的Message!" preferredStyle:UIAlertControllerStyleAlert];
[alertViewController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
textField.backgroundColor = [UIColor redColor];
textField.placeholder = @"请输入用户名";
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didEnterUserName:) name:UITextFieldTextDidChangeNotification object:textField];
}];
[alertViewController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
textField.backgroundColor = [UIColor greenColor];
textField.placeholder = @"请输入密码";
textField.secureTextEntry = YES;
textField.keyboardType = UIKeyboardTypeNumberPad;
}];
UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"登录" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
NSString *userName = alertViewController.textFields[0].text;
NSString *passWord = alertViewController.textFields[1].text;
_labText.text = [NSString stringWithFormat:@"欢迎您,%@!您的密码为:%@",userName,passWord];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UITextFieldTextDidChangeNotification object:nil];
}];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消 " style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
_labText.text = @"点击了--取消按钮, 登录失败!";
[[NSNotificationCenter defaultCenter] removeObserver:self name:UITextFieldTextDidChangeNotification object:nil];
}];
[alertViewController addAction:cancelAction];
[alertViewController addAction:sureAction];
sureAction.enabled = NO;
[self presentViewController:alertViewController animated:YES completion:^{
self.view.backgroundColor = [UIColor blueColor];
}];
}
- (void)didEnterUserName:(NSNotification *)notification
{
UIAlertController *alertController = (UIAlertController *)self.presentedViewController;
if (alertController) {
UITextField *login = alertController.textFields.firstObject;
UIAlertAction *sureAction = alertController.actions.lastObject;
sureAction.enabled = login.text.length > 2;
}
}