背景:这一章让我们来介绍一下ios对话框的展现形式,由简至深的学习
内容:1.普通对话框
2.带有button的对话框
3.带有输入框的对话框
4.带有下拉列表的对话框
关于对话框的内容比较简单,所以废话少说,直接上代码和效果图
1.普通对话框
-(IBAction)buttonPressed2:(id)sender//普通的样式
{
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"标题" message:@"UIAlert默认样式" preferredStyle:UIAlertControllerStyleAlert];
[self presentViewController:alert animated:true completion:nil];
}
2.带有按键的对话框
-(IBAction)buttonPressed1:(id)sender//button的样式
{
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"标题" message:@"UIAlert默认样式" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"cancle" style:UIAlertActionStyleCancel handler:nil];
UIAlertAction * confirmAction = [UIAlertAction actionWithTitle:@"confirm" style:UIAlertActionStyleDefault handler:nil];
[alert addAction:cancelAction];
[alert addAction:confirmAction];
[self presentViewController:alert animated:true completion:nil];
}
带有多个button的对话框
-(IBAction)buttonPressed2:(id)sender//添加button的样式
{
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"标题" message:@"UIAlert默认样式" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"reset" style:UIAlertActionStyleDestructive handler:nil];
UIAlertAction * confirmAction = [UIAlertAction actionWithTitle:@"cancel" style:UIAlertActionStyleDefault handler:nil];
UIAlertAction * confirm1Action = [UIAlertAction actionWithTitle:@"cancel" style:UIAlertActionStyleDefault handler:nil];
UIAlertAction * confirm2Action = [UIAlertAction actionWithTitle:@"cancel" style:UIAlertActionStyleDefault handler:nil];
[alert addAction:cancelAction];
[alert addAction:confirmAction];
[alert addAction:confirm1Action];
[alert addAction:confirm2Action];
[self presentViewController:alert animated:true completion:nil];
}
3.带有输入框的button
-(IBAction)buttonPressed4:(id)sender//添加输入框的样式
{
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"标题" message:@"UIAlert默认样式" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"cancel" style:UIAlertActionStyleCancel handler:nil];
UIAlertAction * confirmAction = [UIAlertAction actionWithTitle:@"confirm" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){
UITextField *login = alert.textFields.firstObject;
UITextField *password = alert.textFields.lastObject;
NSLog(@"LOGIN=%@",login);
NSLog(@"password=%@",password);
[[NSNotificationCenter defaultCenter]removeObserver:self name:UITextFieldTextDidChangeNotification object:nil];
}];
confirmAction.enabled = false;
[alert addAction:cancelAction];
[alert addAction:confirmAction];
[alert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
textField.placeholder = @"登录";
[[NSNotificationCenter defaultCenter ] addObserver:self selector:@selector(alertTextFieldDidChange:) name:UITextFieldTextDidChangeNotification object:textField];
}];
[alert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
textField.placeholder = @"密码";
textField.secureTextEntry = YES;
}];
[self presentViewController:alert animated:true completion:nil];
}
- (void)alertTextFieldDidChange:(NSNotification *)notification{
UIAlertController *alertController = (UIAlertController *)self.presentedViewController;
if (alertController) {
UITextField *login = alertController.textFields.firstObject;
UIAlertAction *okAction = alertController.actions.lastObject;
okAction.enabled = login.text.length > 2;
}
}
4.下拉对话框
-(IBAction)buttonPressed2:(id)sender //下拉对话框的使用
{
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"下拉标题" message:@"下拉信息"
preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
UIAlertAction *confirmAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
UIAlertAction *deleteAction = [UIAlertAction actionWithTitle:@"删除" style:UIAlertActionStyleDestructive handler:nil];
[alert addAction:cancelAction];
[alert addAction:confirmAction];
[alert addAction:deleteAction];
[self presentViewController:alert animated:true completion:nil];
}
参考文章:http://www.cocoachina.com/ios/20141126/10320.html