UIAlertController使用方法、UIAlertAction使用方法(8.0起新控件)

UIAlertController的集成关系:
UIAlertController:UIViewController:UIResponder:NSObject

//viewDidLoad中创建的一个按钮的点击事件
-(void)payBtnAction
{
/*-------------------------------------------UIAlertAction--------------------------------------------------*/
    /**
     1、创建

         + actionWithTitle:style:handler:
     */
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];

    /**
     2、获取Action属性

         .title     //获取标题(只读)
         .style     //获取类型(只读)  UIAlertActionStyleDefault默认、 Cancel取消、 Destructive销毁

         .enabled   //是否可用
     */
    NSLog(@"title:%@",       cancelAction.title);
    NSLog(@"style:%ld",(long)cancelAction.style);

    cancelAction.enabled = NO;


    UIAlertAction *doneAction = [UIAlertAction actionWithTitle:@"完成" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"点击了完成按钮");
    }];





/*------------------------------------------UIAlertController-------------------------------------------------*/
    /**
     1、初始化

         + alertControllerWithTitle:message:preferredStyle:
     */
    UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"My Alert"
                                                                   message:@"This is an alert."
                                                            preferredStyle:UIAlertControllerStyleAlert];


    /**
     2、配置Alert

         .title             //设置和获取标题
         .message           //设置和获取消息

         .preferredStyle    //类型(只读):UIAlertControllerStyleActionSheet,UIAlertControllerStyleAlert
     */
    alert.title = @"Doudou Alert";
    alert.message = @"This is Doudou's alert";


    /**
     3、配置Action

         - addAction:   //添加Action
         .actions       //获取Actions数组(只读)
     */
    [alert addAction:cancelAction];
    [alert addAction:doneAction];


    /**
     4、配置TextField

         - addTextFieldWithConfigurationHandler: //添加输入框,block中配置输入框的属性

         .textFields               //通过[[alert.textFields objectAtIndex:0] text]获取输入框中的内容
     */
    [alert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
        textField.placeholder = @"Login";
    }];
    [alert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
        textField.placeholder = @"Password";
    }];

    NSLog(@"%@",[[alert.textFields objectAtIndex:0] text]);


    /**
     5、显示    //父类中的方法,此方法不能写在viewDidLoad中
     */
    [self presentViewController:alert animated:YES completion:nil];
}

这里写图片描述

这里写图片描述

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值