UIAlertController有两种形式,警告框和操作表,创建步骤都差不多。
1、创建UIAlertController,指定警告和操作样式
2、向其中添加按钮
3、显示UIAlertController
操作表样式:
- (void)createAlertController_sheet{
/*
先创建UIAlertController,preferredStyle:选择UIAlertControllerStyleActionSheet,这个就是相当于创建8.0版本之前的UIActionSheet;
typedef NS_ENUM(NSInteger, UIAlertControllerStyle) {
UIAlertControllerStyleActionSheet = 0,
UIAlertControllerStyleAlert
} NS_ENUM_AVAILABLE_IOS(8_0);
*/
UIAlertController *actionSheet = [UIAlertController alertControllerWithTitle:@"测试标题" message:@"测试内容..." preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"block1回调");
}];
UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"删除" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"block2回调");
}];
UIAlertAction *action3 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"block3回调");
}];
[actionSheetaddAction:action1];
[actionSheetaddAction:action2];
[actionSheetaddAction:action3];
[self presentViewController:actionSheet animated:YES completion:nil];
}
警告样式:
- (void)createAlertController_alert{
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"标题2" message:@"没有内容,随便写点东西" preferredStyle:UIAlertControllerStyleAlert];
[alertaddTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
textField.placeholder=@"输入账号";
}];
[alertaddTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
textField.placeholder=@"输入密码";
}];
UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"block1回调");
//textFields是一个数组,获取所输入的字符串
NSLog(@"%@",alert.textFields.lastObject.text);
}];
UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"block2回调");
}];
[alertaddAction:action1];
[alertaddAction:action2];
[self presentViewController:alert animated:YES completion:nil];
}
网友评论