警告框
iOS中警告框給用戶警告或提示,最多兩個(gè)按鈕,超過(guò)兩個(gè)就應(yīng)該使用操作表。由于在iOS中警告框是模態(tài)的,因此不應(yīng)該隨意使用。
警告框在iOS8之前使用UIAlertView視圖,在iOS8之后推出UIAlertController控制器,可是實(shí)現(xiàn)警告框和操作表。UIAlertController控制器中不僅可以添加按鈕,還可以添加文本框和自定義視圖到警告框和操作表,響應(yīng)事件不用委托協(xié)議實(shí)現(xiàn)。
下面是如何通過(guò)代碼實(shí)現(xiàn)添加警告框:
@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{
//構(gòu)造函數(shù)中第一個(gè)參數(shù)是警告框的標(biāo)題,第二個(gè)參數(shù)是警告框的內(nèi)容,第三個(gè)參數(shù)是一個(gè)枚舉值,表示對(duì)話框類型。枚舉值有兩個(gè)ActionSheet表示操作表,Alert表示警示框,默認(rèn)情況下是操作表
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"警告框" message:@"警告內(nèi)容" preferredStyle:UIAlertControllerStyleAlert];
//UIAlertAction對(duì)象相當(dāng)于一個(gè)按鈕,每往AlertController添加一個(gè)AlertAction就相當(dāng)于添加一個(gè)按鈕。
//此構(gòu)造函數(shù)的第一個(gè)參數(shù)是選項(xiàng)的名稱,第二個(gè)參數(shù)是一個(gè)枚舉值,表示選項(xiàng)樣式,第三個(gè)參數(shù)是參與按鈕動(dòng)作的事件
//第二個(gè)參數(shù)中的枚舉值:Default表示默認(rèn)樣式,粗體顯示標(biāo)題;Cancel,取消按鈕樣式;Destruction,破壞性按鈕樣式,紅色顯示標(biāo)題
UIAlertAction *noAction = [UIAlertAction actionWithTitle:@"No"
style:UIAlertActionStyleCancel
handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"點(diǎn)擊否定按鈕。");
}];
UIAlertAction *yesAction = [UIAlertAction actionWithTitle:@"YES"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"點(diǎn)擊確定按鈕。");
}];
[alertController addAction:noAction];
[alertController addAction:yesAction];
[self presentViewController:alertController animated:YES completion:nil];
}
@end
操作表
如果想給用戶多于兩個(gè)選擇那就需要用到操作表。其布局中最下面的是取消按鈕,最容易被點(diǎn)擊到。如果有一個(gè)是破壞性按鈕,將會(huì)被放在最上面,最不容易按到的地方,并且其顏色為紅色。
下面的代碼就是操作表的實(shí)現(xiàn):
@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(@"點(diǎn)擊了取消按鈕");
}];
UIAlertAction *destructiveAction = [UIAlertAction actionWithTitle:@"破壞性按鈕"
style:UIAlertActionStyleDestructive
handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"點(diǎn)擊了破壞性按鈕");
}];
UIAlertAction *sinaAction = [UIAlertAction actionWithTitle:@"新浪微博"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"點(diǎn)擊了新浪微博按鈕");
}];
UIAlertAction *FaceBookAction = [UIAlertAction actionWithTitle:@"FaceBook"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"點(diǎn)擊了FaceBook按鈕");
}];
[actionSheetController addAction:cancelAction];
[actionSheetController addAction:destructiveAction];
[actionSheetController addAction:FaceBookAction];
[actionSheetController addAction:sinaAction];
[self presentViewController:actionSheetController animated:YES completion:nil];
}
@end
操作表中先添加的AlertAction顯示在操作表的上面,后添加的在下面。