【iOS UI】UIAlertController

【iOS UI】UIAlertController_第1张图片
UIAlertController

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);

UIAlertControllerStyleActionSheet

【iOS UI】UIAlertController_第2张图片
UIAlertControllerStyleActionSheet

UIAlertControllerStyleAlert

【iOS UI】UIAlertController_第3张图片
UIAlertControllerStyleAlert

4、UIAlertActionStyle

typedef NS_ENUM(NSInteger, UIAlertActionStyle) {
    UIAlertActionStyleDefault = 0,
    UIAlertActionStyleCancel,
    UIAlertActionStyleDestructive
} NS_ENUM_AVAILABLE_IOS(8_0);
【iOS UI】UIAlertController_第4张图片
UIAlertActionStyle

1.UIAlertActionStyleDefault 蓝色 细体
2.UIAlertActionStyleCancel 蓝色 粗体
3.UIAlertActionStyleDestructive 红色 细体

你可能感兴趣的:(【iOS UI】UIAlertController)