iOS UIAlertController的使用
2016-05-09 17:30
471 查看
运行环境:
Xcode7.2.1,iOS Simulator9.2
语言:
Objective-C、Swift
关于UIAlertController的使用,主要有三种不同的方式:
下面依次说明一下三种方式的创建和使用。
Swift 版
Swift 版
Swift 版
Xcode7.2.1,iOS Simulator9.2
语言:
Objective-C、Swift
关于UIAlertController的使用,主要有三种不同的方式:
1.简单的UIAlertController形式;
2.带有输入框的UIAlertController;
3.ActionSheet样式的UIAlertController。
下面依次说明一下三种方式的创建和使用。
1.简单的UIAlertController形式
Objective-C 版// 创建一个UIAlertController, 命名为alertOne UIAlertController *alertOne = [UIAlertController alertControllerWithTitle:@"Alert-1" message:@"这是第一个AlertController" preferredStyle:UIAlertControllerStyleAlert]; // 创建一个“取消”按钮 UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]; // 创建一个“确定”按钮 UIAlertAction *confirmAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action){ // 在此写点击“确定”按钮的触发事件 // ...... }]; // 将两个按钮添加到alertOne [alertOne addAction:cancelAction]; [alertOne addAction:confirmAction]; // 弹出alertOne [self presentViewController:alertOne animated:YES completion:nil];
Swift 版
// 创建一个UIAlertController,命名为alertOne let alertOne = UIAlertController.init(title: "Alert-1", message: "这是第一个AlertController", preferredStyle: UIAlertControllerStyle.Alert) // 创建一个“取消”按钮 let cancelAction = UIAlertAction.init(title: "取消", style: UIAlertActionStyle.Cancel, handler: nil) // 创建一个“确定”按钮 let confirmAction = UIAlertAction.init(title: "确定", style: UIAlertActionStyle.Default){ (action: UIAlertAction) -> Void in // 在此写点击“确定”按钮的触发事件 // ...... } // 将两个按钮添加到alertOne alertOne.addAction(cancelAction) alertOne.addAction(confirmAction) // 弹出alertOne self.presentViewController(alertOne, animated: true, completion: nil)
2.带有输入框的UIAlertController
Objective-C 版// 创建一个UIAlertController, 命名为alertTwo UIAlertController *alertTwo = [UIAlertController alertControllerWithTitle:@"Alert-2" message:@"这是第二个AlertController" preferredStyle:UIAlertControllerStyleAlert]; // 给alertTwo添加一个输入框 [alertTwo addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textfield){ textfield.placeholder = @"第一个输入框"; textfield.clearButtonMode = UITextFieldViewModeWhileEditing; }]; // 给alertTwo再添加一个输入框 [alertTwo addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textfield){ textfield.placeholder = @"第二个输入框"; textfield.clearButtonMode = UITextFieldViewModeWhileEditing; }]; // 创建一个“取消”按钮 UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]; // 创建一个“确定”按钮 UIAlertAction *confirmAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action){ // 输出文本框中的内容 NSLog([alertTwo.textFields[0] text]); NSLog([alertTwo.textFields[1] text]); }]; // 将两个按钮添加到alertTwo上 [alertTwo addAction:cancelAction]; [alertTwo addAction:confirmAction]; // 弹出alertTwo [self presentViewController:alertTwo animated:YES completion:nil];
Swift 版
// 创建一个UIAlertController,命名为alertTwo let alertTwo = UIAlertController.init(title: "Alert-2", message: "这是第二个AlertController", preferredStyle: UIAlertControllerStyle.Alert) // 给alertTwo添加第一个输入框 alertTwo.addTextFieldWithConfigurationHandler{ (textfield: UITextField) -> Void in textfield.placeholder = "第一个输入框" } // 给alertTwo添加第二个输入框 alertTwo.addTextFieldWithConfigurationHandler{ (textfield: UITextField) -> Void in textfield.placeholder = "第二个输入框" } // 创建一个“取消”按钮 let cancelAction = UIAlertAction.init(title: "取消", style: UIAlertActionStyle.Cancel, handler: nil) // 创建一个“确定”按钮 let confirmAction = UIAlertAction.init(title: "确定", style: UIAlertActionStyle.Default){ (action: UIAlertAction) -> Void in // 在此写点击“确定”按钮的触发事件 // ...... print(alertTwo.textFields![0].text!) print(alertTwo.textFields![1].text!) } // 将两个按钮添加到alertTwo alertTwo.addAction(cancelAction) alertTwo.addAction(confirmAction) // 弹出alertTwo self.presentViewController(alertTwo, animated: true, completion: nil)
3.ActionSheet样式的UIAlertController
Objective-C 版// 创建一个UIAlertController, 命名为alertThree, preferredStyle 设置为 UIAlertControllerStyleActionSheet UIAlertController *alertThree = [UIAlertController alertControllerWithTitle:@"ActionSheet - 1" message:@"这是一个ActionSheet" preferredStyle:UIAlertControllerStyleActionSheet]; // 创建一个“取消”按钮 UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]; // 创建一个“确定”按钮 UIAlertAction *confirmAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action){ // 在此写点击“确定”按钮的触发事件 // ...... }]; // 将两个按钮添加到alertThree上 [alertThree addAction:cancelAction]; [alertThree addAction:confirmAction]; // 弹出alertThree [self presentViewController:alertThree animated:YES completion:nil];
Swift 版
// 创建一个UIAlertController, 命名为alertThree, preferredStyle 设置为 ActionSheet let alertThree = UIAlertController.init(title: "ActionSheet - 1", message: "这是一个ActionSheet", preferredStyle: UIAlertControllerStyle.ActionSheet) // 创建一个“取消”按钮 let cancelAction = UIAlertAction.init(title: "取消", style: UIAlertActionStyle.Cancel, handler: nil) // 创建一个“确定”按钮 let confirmAction = UIAlertAction.init(title: "确定", style: UIAlertActionStyle.Default){ (action: UIAlertAction) -> Void in // 在此写点击“确定”按钮的触发事件 // ...... } // 将两个按钮添加到alertTwo alertThree.addAction(cancelAction) alertThree.addAction(confirmAction) // 弹出alertThree self.presentViewController(alertThree, animated: true, completion: nil)
相关文章推荐
- [iOS-UI]给输入框添加清除按钮的代码
- 百度编辑器UEditor常用设置函数大全
- error: lvalue required as unary ‘&’ operand
- VS2013error MSB8031: Building an MFC project for a non-Unicode character set is deprecated. You must
- ios 设置UITextField的placeholder大小颜色
- UIButton文字居左显示
- 【Arduino官方教程第一辑】示例程序 3-3 模拟信号输出[Mega]
- UI之登录界面
- mysql Integer Types (Exact Value) - INTEGER, INT, SMALLINT, TINYINT, MEDIUMINT, BIGINT
- 187. Repeated DNA Sequences
- 布局+基本UI
- JS 生成GUID 方法
- UILabel设置行间距
- javax.persistence.TransactionRequiredException: No EntityManager with actual transaction available..
- iOS UIImageView 的使用
- 187. Repeated DNA Sequences 【M】【30】
- iOS 自定义UIButton的title和image位置
- 第十章 UINavigationController 翻译
- 数据库重构工具 LiquiBase(5 Liquibase被锁)
- Liquibase被锁