UIAlertController小示例

ios9新加的UIAlertController,感觉不错挺好用,当做个笔记

- (void)viewDidLoad {

    [super viewDidLoad];

    UIButton * button1 = [[UIButton alloc]initWithFrame:CGRectMake(100, 100, 100, 50)];

    [button1 setTitle:@"ActionSheet" forState:UIControlStateNormal];

    button1.backgroundColor = [UIColor blackColor];

    [button1 addTarget:self action:@selector(showActionSheet) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:button1];

    

    UIButton * button2 = [[UIButton alloc]initWithFrame:CGRectMake(100, 170, 100, 50)];

    [button2 setTitle:@"AlertView" forState:UIControlStateNormal];

    button2.backgroundColor = [UIColor blackColor];

    [button2 addTarget:self action:@selector(showAlertView) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:button2];

}

#pragma mark action sheet

- (void)showActionSheet

{

    UIAlertController * alertC = [UIAlertController alertControllerWithTitle:@"提示" message:@"今天星期四" preferredStyle:UIAlertControllerStyleActionSheet];

    

    UIAlertAction * defaultBtn = [UIAlertAction actionWithTitle:@"普通" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {

        NSLog(@"点击了普通按钮");

    }];

    UIAlertAction * cancelBtn = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {

        NSLog(@"点击了取消按钮");

    }];

    UIAlertAction * destBtn = [UIAlertAction actionWithTitle:@"销毁" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {

        NSLog(@"点击了销毁按钮");

    }];

    

    //添加到actionsheet

    [alertC addAction:defaultBtn];

    [alertC addAction:cancelBtn];

    [alertC addAction:destBtn];

    

    //显示到屏幕

    [self presentViewController:alertC animated:YES completion:nil];

}

#pragma mark alert view

- (void)showAlertView

{

    UIAlertController * alertC = [UIAlertController alertControllerWithTitle:@"提示" message:@"后天星期六是个好日子" preferredStyle:UIAlertControllerStyleAlert];

    

    //添加按钮

    UIAlertAction * cancelBtn = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {

        NSLog(@"点击了取消按钮");

    }];

    

    UIAlertAction * destBtn = [UIAlertAction actionWithTitle:@"重置" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {

        NSLog(@"点击了重置按钮");

        //获取输入框的内容

        UITextField * tf = alertC.textFields[0];

        NSLog(@"%@",tf.text);

        

    }];

    

    //将按钮添加到弹框上

    [alertC addAction:cancelBtn];

    [alertC addAction:destBtn];

    

    //添加输入框

    [alertC addTextFieldWithConfigurationHandler:^(UITextField *textField) {

        textField.placeholder = @"请输入用户名";

    }];

    [alertC addTextFieldWithConfigurationHandler:^(UITextField *textField) {

        textField.placeholder = @"请输入密码";

    }];

    [alertC addTextFieldWithConfigurationHandler:^(UITextField *textField) {

       textField.placeholder = @"请输入银行卡密码";

    }];

    //显示到屏幕上

    [self presentViewController:alertC animated:YES completion:nil];

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值