iOS UIAlertController的简单使用

UIAlertController 是iOS8推出的新概念,用于取代之前的UIActionSheet和UIAlertView。
首先看一下UIAlertControllerStyle

typedef NS_ENUM(NSInteger, UIAlertControllerStyle) {
    UIAlertControllerStyleActionSheet = 0,
    UIAlertControllerStyleAlert
} NS_ENUM_AVAILABLE_IOS(8_0);

UIAlertControllerStyleActionSheet 样式用于取代UIActionSheet、UIAlertControllerStyleAlert 样式用于取代UIAlertView。
UIAlertController 的初始化方法

+ (instancetype)alertControllerWithTitle:(nullable NSString *)title message:(nullable NSString *)message preferredStyle:(UIAlertControllerStyle)preferredStyle

UIAlertActionStyle 样式

typedef NS_ENUM(NSInteger, UIAlertActionStyle) {
    UIAlertActionStyleDefault = 0,
    UIAlertActionStyleCancel,
    UIAlertActionStyleDestructive
} NS_ENUM_AVAILABLE_IOS(8_0);

UIAlertAction 的初始化方法

+ (instancetype)actionWithTitle:(nullable NSString *)title style:(UIAlertActionStyle)style handler:(void (^ __nullable)(UIAlertAction *action))handler;

示例
主界面如下

iOS UIAlertController的简单使用_第1张图片
1.png

代码如下

#import "ViewController.h"
#import "SampleViewController.h"

static NSString * const kMyTableViewCellIdentifier = @"kMyTableViewCellIdentifier";

@interface ViewController ()

// UI
@property (nonatomic, strong) UITableView *myTableView;

// DATA
@property (nonatomic, strong) NSMutableArray *dataArray;

@end

@implementation ViewController

#pragma mark - life cycle
- (void)viewDidLoad
{
    [super viewDidLoad];

    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"示例";
    [self.view addSubview:self.myTableView];
    self.myTableView.frame = self.view.bounds;
}

#pragma mark - UITableViewDataSource method
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.dataArray count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *tempCell = [tableView dequeueReusableCellWithIdentifier:kMyTableViewCellIdentifier];
    tempCell.selectionStyle = UITableViewCellSelectionStyleNone;
    NSString *textStr = [self.dataArray objectAtIndex:indexPath.row];
    tempCell.textLabel.text = textStr;
    
    return tempCell;
}

#pragma mark - UITableViewDelegate method
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 44;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    SampleViewController *sampleVC = [[SampleViewController alloc] init];
    NSString *textStr = [self.dataArray objectAtIndex:indexPath.row];
    if ([textStr isEqualToString:@"UIAlertControllerStyleActionSheet"]) {
        sampleVC.controllerStyle = UIAlertControllerStyleActionSheet;
    } else if ([textStr isEqualToString:@"UIAlertControllerStyleAlert"]) {
        sampleVC.controllerStyle = UIAlertControllerStyleAlert;
    }
    [self.navigationController pushViewController:sampleVC animated:YES];
}

#pragma mark - getter and setter
- (UITableView *)myTableView
{
    if (_myTableView == nil) {
        _myTableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _myTableView.dataSource = self;
        _myTableView.delegate = self;
        [_myTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:kMyTableViewCellIdentifier];
    }
    
    return _myTableView;
}

- (NSMutableArray *)dataArray
{
    if (_dataArray == nil) {
        _dataArray = [[NSMutableArray alloc] init];
        [_dataArray addObject:@"UIAlertControllerStyleActionSheet"];
        [_dataArray addObject:@"UIAlertControllerStyleAlert"];
    }
    
    return _dataArray;
}

@end

点击第一行进入ActionSheet页面如下

iOS UIAlertController的简单使用_第2张图片
2.png

点击点我按钮,弹出ActionSheet,界面如下

iOS UIAlertController的简单使用_第3张图片
3.png

点击第二行进入Alert页面如下

iOS UIAlertController的简单使用_第4张图片
4.png

点击点我按钮,弹出Alert,界面如下

iOS UIAlertController的简单使用_第5张图片
5.png

代码如下

#import "SampleViewController.h"

@interface SampleViewController ()

// UI
@property (nonatomic, strong) UIButton *btn;

@property (nonatomic, strong) UILabel *signLabel;

@end

@implementation SampleViewController

#pragma mark - life cycle
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self.view addSubview:self.btn];
    self.btn.frame = CGRectMake(([UIScreen mainScreen].bounds.size.width - 200) / 2.0, 150, 200, 44);
    [self.view addSubview:self.signLabel];
    self.signLabel.frame = CGRectMake([UIScreen mainScreen].bounds.size.width - 210, 230, 200, 20);
    
    if (self.controllerStyle == UIAlertControllerStyleActionSheet) {
        self.title = @"ActionSheet";
    } else if (self.controllerStyle == UIAlertControllerStyleAlert) {
        self.title = @"Alert";
    }
}

#pragma mark - event response
- (void)btnClicked:(id)sender
{
    if (self.controllerStyle == UIAlertControllerStyleActionSheet) {
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"确定要升级么?" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
        
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"否" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
            NSLog(@"否按钮被点击了");
        }];
        UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"是" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            NSLog(@"是按钮被点击了");
        }];
        UIAlertAction *destructiveAction = [UIAlertAction actionWithTitle:@"关闭" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            NSLog(@"关闭按钮被点击了");
        }];
        
        [alertController addAction:cancelAction];
        [alertController addAction:sureAction];
        [alertController addAction:destructiveAction];
        
        [self presentViewController:alertController animated:YES completion:nil];
    } else if (self.controllerStyle == UIAlertControllerStyleAlert) {
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"提示内容" preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            NSLog(@"取消按钮被点击了");
        }];
        UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            NSLog(@"确定按钮被点击了");
        }];
        
        //修改按钮字体颜色
        [sureAction setValue:[UIColor greenColor] forKey:@"titleTextColor"];
        
        [alertController addAction:cancelAction];
        [alertController addAction:sureAction];
        
        [self presentViewController:alertController animated:YES completion:nil];
    }
}

#pragma mark - getter and setter
- (UIButton *)btn
{
    if (_btn == nil) {
        _btn = [UIButton buttonWithType:UIButtonTypeCustom];
        _btn.backgroundColor = [UIColor greenColor];
        [_btn addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];
        [_btn setTitle:@"点我" forState:UIControlStateNormal];
    }
    
    return _btn;
}

- (UILabel *)signLabel
{
    if (_signLabel == nil) {
        _signLabel = [[UILabel alloc] init];
        _signLabel.backgroundColor = [UIColor clearColor];
        _signLabel.font = [UIFont systemFontOfSize:15];
        _signLabel.text = @"By 点滴86";
        _signLabel.textAlignment = NSTextAlignmentRight;
    }
    
    return _signLabel;
}

@end

UIAlertController 的简单使用就到此结束啦

你可能感兴趣的:(iOS UIAlertController的简单使用)