创建一个button调用控件
- (void)viewDidLoad {
[superviewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIButton *button = [UIButtonbuttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(100, 100, 100, 30);
button.backgroundColor = [UIColorredColor];
[button addTarget:selfaction:@selector(buttonclick:)forControlEvents:UIControlEventTouchUpInside];
[self.viewaddSubview:button];
}
//button的方法实现
-(void)buttonclick:(UIButton *)sender{
UIAlertController *alertController = [UIAlertControlleralertControllerWithTitle:@"yes"message:@"没有message"preferredStyle:UIAlertControllerStyleActionSheet];
// preferredStyle 为样式,,有两个可以选择 UIAlertControllerStyleActionSheet 样式为actionsheet的样式 UIAlertControllerStyleAlert为alertview的样式
UIAlertAction *Sure = [UIAlertActionactionWithTitle:@"确定"style:(UIAlertActionStyleDefault)handler:^(UIAlertAction *action) {
//添加确定的点击事件 点击事件写在这里
}];//可以添加多个确定键
UIAlertAction *cancel = [UIAlertActionactionWithTitle:@"取消"style:(UIAlertActionStyleCancel)handler:^(UIAlertAction *action) {
//添加取消的点击事件
}];//只能添加一个取消按钮
UIAlertAction *alert = [UIAlertActionactionWithTitle:@"警告"style:(UIAlertActionStyleDestructive)handler:^(UIAlertAction *action) {
//添加警告的点击事件
}];
// [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
// textField.placeholder = @"填写";
//
// }]; //只有在alertView的样式下才可以添加
[alertController addAction:Sure];
[alertController addAction:cancel];
[alertController addAction:alert];
[selfpresentViewController:alertControlleranimated:YEScompletion:nil];
}
demo下载地址 https://github.com/yongchangye/ios9AlertView