UIAlertController的使用——创建提示窗口


UIAlertController只能在iOS8以上使用,所以要慎用。

在iOS8中,UIAlertController在功能上是和UIAlertView以及UIActionSheet相同的,UIAlertController以一种模块化替换的方式来代替这两货的功能和作用。是使用对话框(alert)还是使用上拉菜单(action sheet),就取决于在创建控制器时,您是如何设置首选样式的。(其实宝宝们心里都知道大笑大笑





- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

    //创建按钮

    UIButton *bt=[UIButton buttonWithType:UIButtonTypeCustom];

    bt.frame =CGRectMake(50, 50, 100, 50);

    [bt addTarget:self action:@selector(bt) forControlEvents:UIControlEventTouchDown];

    [bt setBackgroundColor:[UIColor cyanColor]];

    [self.view addSubview:bt];

    [self.view setBackgroundColor:[UIColor yellowColor]];


 

}

//按钮的点击事件

-(void)bt


{

 

 //UIAlertController在功能上是和UIAlertView以及UIActionSheet相同的,UIAlertController以一种模块化替换的方式来代替这两货的功能和作用。是使用对话框(alert)还是使用上拉菜单(action sheet),就取决于在创建控制器时,您是如何设置首选样式的。

   

 //常规(default)、取消(cancel)以及警示(destruective

#pragma mark - 对话框


    NSString *title = @"注意";

    NSString *message = @"姓名和电话";

    // NSString *cancelButtonTitle = @"Cancel";

    NSString *otherButtonTitle = @"确定";

    NSString *bbTitle=@"xx";

//

    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];

//    // UIAlertAction *cancel = [UIAlertAction actionWithTitle:cancelButtonTitle style:UIAlertActionStyleCancel handler:nil];

    UIAlertAction *otherA = [UIAlertAction actionWithTitle:otherButtonTitle style:UIAlertActionStyleDefault handler:nil];

    UIAlertAction *action=[UIAlertAction actionWithTitle:bbTitle style:UIAlertActionStyleDefault handler:nil];

//    //[alertController addAction:cancel];

    

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

        NSLog(@"取消");

    }];

    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"好的" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {

        

        NSLog(@"好的");

        

    }];

    

    

    

  // 带输入框的文本对话框

    

    [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField){

        textField.placeholder = @"账号";

    }];

    [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {

        

        textField.placeholder = @"密码";

        

        textField.secureTextEntry = YES;

    }];

    

    

    //将创建的按钮添加到控制器上

    [alertController addAction:cancelAction];

    [alertController addAction:okAction];

    [alertController addAction:otherA];

    [alertController addAction:action];

  

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

  



#pragma mark - 上拉菜单

 

    UIAlertController *alertController=[UIAlertController alertControllerWithTitle:@"标题" message:@"默认" preferredStyle:UIAlertControllerStyleActionSheet];

    

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

    UIAlertAction *okAction=[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){

        NSLog(@"ok");

    }];

    [alertController addAction:cancelAction];

    [alertController addAction:okAction];

    

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

  



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值