zoukankan      html  css  js  c++  java
  • iOS iOS8新特性--UIAlertController

    1. iOS7及iOS7之前警告类控件有UIAlertView和UIActionSheet

    1.1 UIAlertView的使用

        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"警告" message:@"这是一个UIAlertView" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:@"关闭", nil];

        alert.alertViewStyle = UIAlertViewStylePlainTextInput;

        /**   alertViewStyle的枚举值如下

         UIAlertViewStyleDefault = 0,

         UIAlertViewStyleSecureTextInput,

         UIAlertViewStylePlainTextInput,

         UIAlertViewStyleLoginAndPasswordInput

         */

        [alert show];

    //  UIAlertViewDelegate

    - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex;

    1.2 UIActionSheet的使用

        UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@"这是一个UIActionSheet" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"确定"

                                                  otherButtonTitles:@"关闭", nil];

        [sheet showInView:self.view];

    // UIActionSheetDelegate

    - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex

    2. iOS8开始,用UIAlertController替代UIAlertView+UIActionSheet

    2.1 UIAlertController的使用 (Style: UIAlertControllerStyleAlert)

        // 初始化UIAlertController

      UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"警告" message:@"iOS新控件UIAlertController" preferredStyle:UIAlertControllerStyleAlert];

        // 添加按钮,注意如果Block的循环引用

        __weak typeof(alert) weakAlert = alert;

        [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {

        // block内部写点击按钮之后做的事情

        NSLog(@"点击了确定按钮--%@-%@", [weakAlert.textFields.firstObject text], [weakAlert.textFields.lastObject text]);

        }]];

        // 添加文本框

        [alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {

            textField.textColor = [UIColor blueColor];

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

            [textField addTarget:self action:@selector(usernameDidChange:) forControlEvents:UIControlEventEditingChanged];

             // 也可以用通知监听TextField的变化

         //[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(usernameDidChange:) name:UITextFieldTextDidChangeNotification object:textField];

        }];

        [alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {

            textField.secureTextEntry = YES;

            textField.text = @"123456";

        }];

        // 弹出UIAlertController

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

    // textField EditingChanged时,回调的方法

    - (void)usernameDidChange:(UITextField *)username

    {

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

    }

    2.2 UIAlertController的使用 (Style: UIAlertControllerStyleActionSheet)

        // 在2.1代码上,修改 初始化UIAlertController 的代码

      UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"警告" message:@"iOS新控件UIAlertController" preferredStyle:

    UIAlertControllerStyleActionSheet];

    运行程序后报错,因为text field只能用在Style:UIAlertControllerStyleAlertUIAlertController上,把添加textFiled的代码注销掉,再运行程序就正常了

    reason: 'Text fields can only be added to an alert controller of style UIAlertControllerStyleAlert'

    2.3通过上面的介绍,可以看出苹果有意把UIAlertView,UIActionSheet统一在一起,实际上,苹果还统一了iphone和ipad的UIAlertController的处理方式

    2.3.1 新建项目时,Devices选择Universal,语言选OC(截图错误)

    2.3.2 在Main.storyboard中拖导航控制器,让mainViewController成为导航控制器的rootViewController

    2.3.3 代码示例

      // 初始化UIAlertController

        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"警告" message:@"iOS新控件UIAlertController" preferredStyle:

    UIAlertControllerStyleAlert];

        // 设置popover指向的item

        alert.popoverPresentationController.barButtonItem = self.navigationItem.leftBarButtonItem;

        // 添加按钮

        [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {

            NSLog(@"点击了确定按钮");

        }]];

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

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

        }]];

        // 弹出UIAlertController

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

    注:左图是在iphone上运行效果,右图是在ipad上的运行效果,Style为UIAlertControllerStyleAlert时,显示样式一致

      

    2.3.4 把2.3.3的初始化代码改成Style:UIAlertControllerStyleActionSheet

        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"警告" message:@"iOS新控件UIAlertController" preferredStyle:UIAlertControllerStyleActionSheet];

     注:左图是iphone,右图是ipad,用一份代码实现了在iphone和ipad上不同的显示样式
      

  • 相关阅读:
    数据产品—数据仓库
    数据产品-开篇
    os.walk()
    pytest入门
    XML 文件处理
    字符编码
    消息队列
    Pycharm
    AWS入门
    Python配置模块:configparser参数含义
  • 原文地址:https://www.cnblogs.com/oumygade/p/4274323.html
Copyright © 2011-2022 走看看