#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
CGRect screen = [[UIScreen mainScreen] bounds];
//设置按钮
UIButton* buttonActionSheet = [UIButton buttonWithType:UIButtonTypeSystem];
[buttonActionSheet setTitle:@"Test操作表" forState:UIControlStateNormal];
CGFloat buttonActionSheetWidth = 100;
CGFloat buttonActionSheetHeight = 30;
CGFloat buttonActionSheetLeftView = (screen.size.width - buttonActionSheetWidth)/2;
CGFloat buttonActionSheetTopView = 260;
buttonActionSheet.frame = CGRectMake(buttonActionSheetLeftView, 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* cancleAction = [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:cancleAction];
[actionSheetController addAction:destructiveAction];
[actionSheetController addAction:otherAction];
//为ipad设备设置浮动层锚点
actionSheetController.popoverPresentationController.sourceView = sender;
//show
[self presentViewController:actionSheetController animated:true completion:nil];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end