UIAlertController相关
- (void)viewDidLoad {
[super viewDidLoad];
//创建两个button
[self createTwoButton];
//创建一个警告框
[self createOneAlertCon];
//创建一个操作表
[self createOneSheetCon];
}
-(void)createTwoButton{
CGRect screen = [[UIScreen mainScreen] bounds];
UIButton* buttonAlertView = [UIButton buttonWithType:UIButtonTypeSystem];
[buttonAlertView setTitle:@"警告框" 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(createOneAlertCon) forControlEvents: UIControlEventTouchUpInside];
[self.view addSubview:buttonAlertView];
UIButton* buttonActionSheet = [UIButton buttonWithType:UIButtonTypeSystem];
[buttonActionSheet setTitle:@"操作表" 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(createOneSheetCon) forControlEvents: UIControlEventTouchUpInside];
[self.view addSubview:buttonActionSheet];
}
#pragma mark - 创建一个警告框
-(void)createOneAlertCon{
UIAlertController* alertController = [UIAlertController alertControllerWithTitle:@"Alert" message: @"Alert text goes here" preferredStyle:UIAlertControllerStyleAlert];
// UIAlertControllerStyleActionSheet = 0,//默认类型 操作表
// UIAlertControllerStyleAlert//警告框
UIAlertAction* noAction = [UIAlertAction actionWithTitle:@"No" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
NSLog(@"Tap No Button");
}];
UIAlertAction* yesAction = [UIAlertAction actionWithTitle:@"Yes" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
NSLog(@"Tap Yes Button");
}];
UIAlertAction* otherAction = [UIAlertAction actionWithTitle:@"other" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
NSLog(@"Tap other Button");
}];
[alertController addAction:noAction];
[alertController addAction:yesAction];
// [alertController addAction:otherAction];
//显示
[self presentViewController:alertController animated:TRUE completion:nil];
}
#pragma mark - 创建一个操作表
-(void)createOneSheetCon{
UIAlertController* actionSheetController = [[UIAlertController alloc] init];//默认创建的是UIAlertControllerStyleActionSheet类型
UIAlertAction* cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
NSLog(@"Tap 取消 Button");
}];
UIAlertAction* destructiveAction = [UIAlertAction actionWithTitle:@"破坏性按钮" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
NSLog(@"Tap 破坏性按钮 Button");
}];
UIAlertAction* otherAction = [UIAlertAction actionWithTitle:@"新浪微博" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
NSLog(@"Tap 新浪微博 Button");
}];
[actionSheetController addAction:cancelAction];
[actionSheetController addAction:destructiveAction];
[actionSheetController addAction:otherAction];
//显示
[self presentViewController:actionSheetController animated:TRUE completion:nil];
}