注:在iPad中,操作表的布局与iPhone不同,操作表不是从底部滑出的,而是随机的出现在触发它的按钮的周围,而且不能添加取消按钮,如果添加也不给予显示。
- (void)ActionSheetDemo{
//操作表
//UIAlertController 的枚举成员UIAlertControllerStyleActionSheet
UIButton *buttonActionSheet = [[UIButton alloc] initWithFrame:CGRectMake(130, 300, 120, 45)];
buttonActionSheet.backgroundColor = [UIColor grayColor];
buttonActionSheet.layer.cornerRadius = 10;
[buttonActionSheet setTitle:@"操作表" forState:UIControlStateNormal];
[buttonActionSheet addTarget:self action:@selector(showActionSheetView:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:buttonActionSheet];
}
- (void)showActionSheetView:(id)sender{
NSLog(@"%s", __func__);
//创建初始化
UIAlertController *actionSheetController = [[UIAlertController alloc] init];
//创建两个UIAlertAction(标题,样式,按钮动作相关的闭包)
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action){
NSLog(@"Tap cancel 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];
}