警告框
iOS中警告框给用户警告或提示,最多两个按钮,超过两个就应该使用操作表。由于在iOS中警告框是模态的,因此不应该随意使用。
警告框在iOS8之前使用UIAlertView视图,在iOS8之后推出UIAlertController控制器,可是实现警告框和操作表。UIAlertController控制器中不仅可以添加按钮,还可以添加文本框和自定义视图到警告框和操作表,响应事件不用委托协议实现。
下面是如何通过代码实现添加警告框:
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
CGRect screen = [UIScreen mainScreen].bounds;
UIButton *buttonAlertView = [UIButton buttonWithType:UIButtonTypeSystem];
[buttonAlertView setTitle:@"Test警告框" forState:UIControlStateNormal];
CGFloat buttonAlertViewWidth = 100;
CGFloat buttonAlertViewHeight = 30;
CGFloat buttonAlertViewTopView = 130;
buttonAlertView.frame = CGRectMake((screen.size.width - buttonAlertViewWidth)/2, buttonAlertViewTopView, buttonAlertViewWidth, buttonAlertViewHeight);
[buttonAlertView addTarget:self action:@selector(testAlertView:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:buttonAlertView];
}
- (void)testAlertView:(id)sender{
//构造函数中第一个参数是警告框的标题,第二个参数是警告框的内容,第三个参数是一个枚举值,表示对话框类型。枚举值有两个ActionSheet表示操作表,Alert表示警示框,默认情况下是操作表
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"警告框" message:@"警告内容" preferredStyle:UIAlertControllerStyleAlert];
//UIAlertAction对象相当于一个按钮,每往AlertController添加一个AlertAction就相当于添加一个按钮。
//此构造函数的第一个参数是选项的名称,第二个参数是一个枚举值,表示选项样式,第三个参数是参与按钮动作的事件
//第二个参数中的枚举值:Default表示默认样式,粗体显示标题;Cancel,取消按钮样式;Destruction,破坏性按钮样式,红色显示标题
UIAlertAction *noAction = [UIAlertAction actionWithTitle:@"No"
style:UIAlertActionStyleCancel
handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"点击否定按钮。");
}];
UIAlertAction *yesAction = [UIAlertAction actionWithTitle:@"YES"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"点击确定按钮。");
}];
[alertController addAction:noAction];
[alertController addAction:yesAction];
[self presentViewController:alertController animated:YES completion:nil];
}
@end
操作表
如果想给用户多于两个选择那就需要用到操作表。其布局中最下面的是取消按钮,最容易被点击到。如果有一个是破坏性按钮,将会被放在最上面,最不容易按到的地方,并且其颜色为红色。
下面的代码就是操作表的实现:
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
CGRect screen = [UIScreen mainScreen].bounds;
UIButton *buttonActionSheet = [UIButton buttonWithType:UIButtonTypeSystem];
[buttonActionSheet setTitle:@"Test操作表" forState:UIControlStateNormal];
CGFloat buttonActionSheetWidth = 100;
CGFloat buttonActionSheetHeight = 30;
CGFloat buttonActionSheetTopView = 260;
buttonActionSheet.frame = CGRectMake((screen.size.width - buttonActionSheetWidth)/2, buttonActionSheetTopView, buttonActionSheetWidth, buttonActionSheetHeight);
[buttonActionSheet addTarget:self action:@selector(testActionSheet:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:buttonActionSheet];
}
- (void)testActionSheet:(id)sender{
UIAlertController *actionSheetController = [[UIAlertController alloc]init];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消"
style:UIAlertActionStyleCancel
handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"点击了取消按钮");
}];
UIAlertAction *destructiveAction = [UIAlertAction actionWithTitle:@"破坏性按钮"
style:UIAlertActionStyleDestructive
handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"点击了破坏性按钮");
}];
UIAlertAction *sinaAction = [UIAlertAction actionWithTitle:@"新浪微博"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"点击了新浪微博按钮");
}];
UIAlertAction *FaceBookAction = [UIAlertAction actionWithTitle:@"FaceBook"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"点击了FaceBook按钮");
}];
[actionSheetController addAction:cancelAction];
[actionSheetController addAction:destructiveAction];
[actionSheetController addAction:FaceBookAction];
[actionSheetController addAction:sinaAction];
[self presentViewController:actionSheetController animated:YES completion:nil];
}
@end
操作表中先添加的AlertAction显示在操作表的上面,后添加的在下面。