美文网首页
在iOS8中如何使用UIAlertController来替换UI

在iOS8中如何使用UIAlertController来替换UI

作者: coderpaul | 来源:发表于2015-12-07 09:59 被阅读353次

    在iOS8中UIAlertController已经取代UIActionsheet UIAlertView。
    可以创建一个AlertController“标题”和“消息”来作为警告框。

    UIAlertController的两种设置格式:

    • UIAlertControllerStyleActionSheet
      用于配置UIAlertController ActionSheet
    • UIAlertControllerStyleAlert
      用于配置UIAlertController警报。

    1.创建一个alert对象和一个actionsheet对象,请看以下code:

    • 创建AlertController对象:
    UIAlertController *alertView = [UIAlertController alertControllerWithTitle:@"title"
                                                                                                                                 message:@"Select a title"
                                                                                                                      preferredStyle:UIAlertControllerStyleAlert];
    
    • 添加action:
      [alertView addAction:[UIAlertAction actionWithTitle:@"ok"
                                                                                           style:UIAlertActionStyleDefault
                                                                                       handler:^(UIAlertAction *action) {
                                                                                                        //要做的事情
                                                                                               }]];
    

    在你点击‘ok’的时候会回调handler这个block

    • 显示alertController
    [self presentViewController:alertView animated:YES completion:{
    BJLog("show the alertView");
    }];
    

    2.创建一个简单的提醒OK按钮和消息

    下面的这个方法将创建一个简单的AlertController在iOS8警报

    - (void)showAlertView
    {
    //创建alertcontroller对象
       UIAlertController *alertView = [UIAlertController alertControllerWithTitle:@"simple" message:@"This is an simple AlertView" perferredStyle:UIAlertControllerAlert];
       //添加action
       [alertView addAction:[UIAlertAction actionWithTitle:@"ok" style:UIAlertActionStyleDefault
       handler:^(UIAlertAction *action) {                                                                                                       //要做的事情
       BJLog(@"action - %@", action.title);  
                                                                                                                                  }]];   
         //显示alertView
         [self presentViewController:alertView animated:YES completion:^{
        NSLog(@"alertView shown!");
    }];                                                                                                                                                                                                                                              
    }
    

    3.创建一个警告文本框:

    - (void)showAlertWithTextField {
    
    UIAlertController *alertView = [UIAlertController alertControllerWithTitle:@"Menu"
                                                                       message:@"Select a menu"
                                                                preferredStyle:UIAlertControllerStyleAlert];
    
    [alertView addTextFieldWithConfigurationHandler:^(UITextField *textField) {
        //Customize the textField
        textField.placeholder = @"User name";
        textField.textColor = [UIColor whiteColor];
        textField.borderStyle = UITextBorderStyleRoundedRect;
        textField.backgroundColor = [UIColor colorWithRed:171/255 green:182/255 blue:255/255 alpha:0.5];
    }];
    
    [alertView addAction:[UIAlertAction actionWithTitle:@"ok"
                                                  style:UIAlertActionStyleDefault
                                                handler:^(UIAlertAction *action) {
                                                    BJLog(@"action - %@", action.title);//callback
                                                }]];
    
    [self presentViewController:alertView animated:YES completion:^{
        BJLog(@"completed");
    }];
    

    }

    4.给AlertController添加两个文本框:

    - (void)showActionSheetWithTwoButtons {
    
    UIAlertController *alertView = [UIAlertController alertControllerWithTitle:@"ActionSheet!"
                                                                       message:@"This is an ActionSheet."
                                                          preferredStyle:UIAlertControllerStyleActionSheet];
    
    [alertView addAction:[UIAlertAction actionWithTitle:@"ok"
                                                  style:UIAlertActionStyleDefault
                                                handler:^(UIAlertAction *action) {
                                                    NSLog(@"action - %@", action.title);
                                                }]];
    
    [alertView addAction:[UIAlertAction actionWithTitle:@"Cancel"
                                                  style:UIAlertActionStyleDefault
                                                handler:^(UIAlertAction *action) {
                                                    NSLog(@"action - %@", action.title);
                                                }]];
    
    [self presentViewController:alertView animated:YES completion:^{
        NSLog(@"alertView shown!");
    }];
    
    }
    

    代码比较简单,就不让代码君出来玩了!

    相关文章

      网友评论

          本文标题:在iOS8中如何使用UIAlertController来替换UI

          本文链接:https://www.haomeiwen.com/subject/rikehttx.html