1、创建Button绑定alert方法
self.btn = [UIButton buttonWithType:UIButtonTypeCustom];
self.btn.backgroundColor = [UIColor orangeColor];
self.btn.frame = CGRectMake(100, 100, 200, 200);
[self.btn setTitle:@"Alert" forState:UIControlStateNormal];
[self.btn addTarget:self action:@selector(alert) forControlEvents:UIControlEventTouchDown];
[self.view addSubview:self.btn];
2、alert方法
- (void)alert{
// 初始化AlertController
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"AlertControllerTitle" message:@"AlertControllerMessage" preferredStyle:UIAlertControllerStyleAlert];
// UIAlertActionStyleDefault
UIAlertAction *firstAction = [UIAlertAction actionWithTitle:@"1" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
}];
// UIAlertActionStyleCancel
UIAlertAction *secondAction = [UIAlertAction actionWithTitle:@"2" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
}];
// UIAlertActionStyleDestructive
UIAlertAction *thirdAction = [UIAlertAction actionWithTitle:@"3" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
}];
// 添加action
[alert addAction:firstAction];
[alert addAction:secondAction];
[alert addAction:thirdAction];
// 显示
[self presentViewController:alert animated:YES completion:nil];
}
3、UIAlertControllerStyle
typedef NS_ENUM(NSInteger, UIAlertControllerStyle) {
UIAlertControllerStyleActionSheet = 0,
UIAlertControllerStyleAlert
} NS_ENUM_AVAILABLE_IOS(8_0);
UIAlertControllerStyleActionSheetUIAlertControllerStyleActionSheet
UIAlertControllerStyleAlertUIAlertControllerStyleAlert
4、UIAlertActionStyle
typedef NS_ENUM(NSInteger, UIAlertActionStyle) {
UIAlertActionStyleDefault = 0,
UIAlertActionStyleCancel,
UIAlertActionStyleDestructive
} NS_ENUM_AVAILABLE_IOS(8_0);
UIAlertActionStyle
1.UIAlertActionStyleDefault 蓝色 细体
2.UIAlertActionStyleCancel 蓝色 粗体
3.UIAlertActionStyleDestructive 红色 细体
网友评论