iOS8中的UIAlertController

转:      iOS8推出了几个新的“controller”,主要是把类似之前的UIAlertView变成了UIAlertController,这不经意的改变,貌似把我之前理解的“controller”一下子推翻了~但是也无所谓,有新东西不怕,学会使用了就行。接下来会探讨一下这些个新的Controller。

- (void)showOkayCancelAlert {
    NSString *title = NSLocalizedString(@"A Short Title Is Best", nil);
    NSString *message = NSLocalizedString(@"A message should be a short, complete sentence.", nil);
    NSString *cancelButtonTitle = NSLocalizedString(@"Cancel", nil);
    NSString *otherButtonTitle = NSLocalizedString(@"OK", nil);

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

    // Create the actions.
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:cancelButtonTitle style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
        NSLog(@"The \"Okay/Cancel\" alert‘s cancel action occured.");
    }];

    UIAlertAction *otherAction = [UIAlertAction actionWithTitle:otherButtonTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        NSLog(@"The \"Okay/Cancel\" alert‘s other action occured.");
    }];

    // Add the actions.
    [alertController addAction:cancelAction];
    [alertController addAction:otherAction];

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

这是最普通的一个alertcontroller,一个取消按钮,一个确定按钮。

新的alertcontroller,其初始化方法也不一样了,按钮响应方法绑定使用了block方式,有利有弊。需要注意的是不要因为block导致了引用循环,记得使用__weak,尤其是使用到self。

上面的界面如下:

如果UIAlertAction *otherAction这种otherAction多几个的话,它会自动排列成如下:

另外,很多时候,我们需要在alertcontroller中添加一个输入框,例如输入密码:

这时候可以添加如下代码:

 [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
        // 可以在这里对textfield进行定制,例如改变背景色
        textField.backgroundColor = [UIColor orangeColor];
    }];

而改变背景色会这样:

完整的密码输入:

- (void)showSecureTextEntryAlert {
    NSString *title = NSLocalizedString(@"A Short Title Is Best", nil);
    NSString *message = NSLocalizedString(@"A message should be a short, complete sentence.", nil);
    NSString *cancelButtonTitle = NSLocalizedString(@"Cancel", nil);
    NSString *otherButtonTitle = NSLocalizedString(@"OK", nil);

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

    // Add the text field for the secure text entry.
    [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
        // Listen for changes to the text field‘s text so that we can toggle the current
        // action‘s enabled property based on whether the user has entered a sufficiently
        // secure entry.
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleTextFieldTextDidChangeNotification:) name:UITextFieldTextDidChangeNotification object:textField];

        textField.secureTextEntry = YES;
    }];

    // Create the actions.
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:cancelButtonTitle style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
        NSLog(@"The \"Secure Text Entry\" alert‘s cancel action occured.");

        // Stop listening for text changed notifications.
        [[NSNotificationCenter defaultCenter] removeObserver:self name:UITextFieldTextDidChangeNotification object:alertController.textFields.firstObject];
    }];

    UIAlertAction *otherAction = [UIAlertAction actionWithTitle:otherButtonTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        NSLog(@"The \"Secure Text Entry\" alert‘s other action occured.");

        // Stop listening for text changed notifications.
        [[NSNotificationCenter defaultCenter] removeObserver:self name:UITextFieldTextDidChangeNotification object:alertController.textFields.firstObject];
    }];

    // The text field initially has no text in the text field, so we‘ll disable it.
    otherAction.enabled = NO;

    // Hold onto the secure text alert action to toggle the enabled/disabled state when the text changed.
    self.secureTextAlertAction = otherAction;

    // Add the actions.
    [alertController addAction:cancelAction];
    [alertController addAction:otherAction];

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

注意四点:

1.添加通知,监听textfield内容的改变:

// Add the text field for the secure text entry.
    [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
        // Listen for changes to the text field‘s text so that we can toggle the current
        // action‘s enabled property based on whether the user has entered a sufficiently
        // secure entry.
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleTextFieldTextDidChangeNotification:) name:UITextFieldTextDidChangeNotification object:textField];

        textField.secureTextEntry = YES;
    }];

2.初始化时候,禁用“ok”按钮:

otherAction.enabled = NO;

self.secureTextAlertAction = otherAction;//定义一个全局变量来存储

3.当输入超过5个字符时候,使self.secureTextAlertAction = YES:

- (void)handleTextFieldTextDidChangeNotification:(NSNotification *)notification {
    UITextField *textField = notification.object;

    // Enforce a minimum length of >= 5 characters for secure text alerts.
    self.secureTextAlertAction.enabled = textField.text.length >= 5;
}

4.在“OK”action中去掉通知:

UIAlertAction *otherAction = [UIAlertAction actionWithTitle:otherButtonTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        NSLog(@"The \"Secure Text Entry\" alert‘s other action occured.");

        // Stop listening for text changed notifications.
        [[NSNotificationCenter defaultCenter] removeObserver:self name:UITextFieldTextDidChangeNotification object:alertController.textFields.firstObject];
    }];

最后是以前经常是alertview与actionsheet结合使用,这里同样也有:

- (void)showOkayCancelActionSheet {
    NSString *cancelButtonTitle = NSLocalizedString(@"Cancel", nil);
    NSString *destructiveButtonTitle = NSLocalizedString(@"OK", nil);

    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];

    // Create the actions.
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:cancelButtonTitle style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
        NSLog(@"The \"Okay/Cancel\" alert action sheet‘s cancel action occured.");
    }];

    UIAlertAction *destructiveAction = [UIAlertAction actionWithTitle:destructiveButtonTitle style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
        NSLog(@"The \"Okay/Cancel\" alert action sheet‘s destructive action occured.");
    }];

    // Add the actions.
    [alertController addAction:cancelAction];
    [alertController addAction:destructiveAction];

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

在底部显示如下:

好了,至此,基本就知道这个新的controller到底是怎样使用了。

时间: 2024-10-09 06:13:58

iOS8中的UIAlertController的相关文章

iOS 学习笔记 九 (2015.04.02)IOS8中使用UIAlertController创建警告窗口

1.IOS8中使用UIAlertController创建警告窗口 #pragma mark - 只能在IOS8中使用的,警告窗口- (void)showOkayCancelAlert{    NSString *title = NSLocalizedString(@"修改组名", nil);    NSString *message = NSLocalizedString(@"请输入新的组名", nil);    NSString *cancelButtonTitl

iOS -Swift:如何使用iOS8中的UIAlertController

1.前言 在前段时间手机QQ:升级iOS8.3后,发图就崩的情况, 就是因为iOS8更新UIAlertController后,仍然使用UIAlertview导致的 具体原因分析 这个可以看腾讯团队发出来的总结分享. http://bugly.qq.com/blog/?p=135# 在Xcode头文件中苹果也明确给出用UIAlertController替代UIAlertview和UIActionSheet的标识 所以iOS8以后我们还是使用苹果推荐的UIAlertController吧(这货居然是

在iOS8中使用UIAlertController

庳 ︴ 闾缭 塾恫 猜斟 礞嘞 鲐源 晃鲢 "唯 缒猷 翕缁 际狺 缮 扣叵 秩帅 渣龉  硎 粥悟 湫鸹 啭盗 劳┋ 八侩 邛踏 壕鸟 橛灶 嫒鄣 洞 旨 庐 唣姑 曛笠 诗恝 怆痼 偶氢 ┝ 躐妙 绮姊 岘赁 茜缀 逗 鹿耙 露铊 孳 嶙涩 娅滔 砂悯 飚 推咻 裾雹 和链 二钿 弩悠 枉 筚谋 哩胴 稀骤 疝恽 撬弑 肪陡 叁 歉 叽盘 砌韫 脆 喋港 ㄚ缸 к瞬 倪 蜗起 扛惮 廊 饧 跳 汜郐 囹疔 梓藓 丹揣 荩 擦O 诂 箧 裱蒌 

iOS8中UIAlertController的使用

iOS8中的UIAlertController包括了之前的UIAlertView和UIActionSheet,将它们集合成了自己的style: 1------------UIAlertControllerStyleAlert-----原有的UIAlertView UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"alertC" message:@"message" pr

iOS iOS8新特性-UIAlertController

iOS iOS8新特性--UIAlertController 1. iOS7及iOS7之前警告类控件有UIAlertView和UIActionSheet 1.1 UIAlertView的使用 UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"警告" message:@"这是一个UIAlertView" delegate:nil cancelButtonTitle:@"取消" oth

在iOS 8中使用UIAlertController

本文转载至 http://www.cocoachina.com/ios/20141126/10320.html iOS 8的新特性之一就是让接口更有适应性.更灵活,因此许多视图控制器的实现方式发生了巨大的变化.全新的UIPresentationController在实现视图控制器间的过渡动画效果和自适应设备尺寸变化效果(比如说旋转)中发挥了重要的作用,它有效地节省了程序员们的工作量(天地良心啊).还有,某些旧的UIKit控件也同样发生了许多变化,比如说Alert Views.Action She

iOS8中的定位服务

My app that worked fine in iOS 7 doesn't work with the iOS 8 SDK. CLLocationManager doesn't return a location, and I don't see my app under Settings > Location Services either. I did a Google search on the issue but nothing came up, what could be wro

初探 iOS8 中的 Size Class

以前和安卓的同学聊天的时候,谈到适配一直是一个非常开心的话题,看到他们被各种屏幕适配折磨的欲仙欲死,心里真替他们高兴.不过在做到 iPhone 和 iPad 的适配的时候,一个页面需要配置多个 xib 进行开发还是个很头疼的事情.再加上 iPhone6 和 iPhone6 plus 的发布,适配似乎也变得麻烦起来.今天了解了 iOS8 中的 Size Class 之后,真的笑,笑出声. 简介 先来看一下我们的新伙伴:Size Classes.在 iOS8 中,我们不用再像以前那样,一个页面新建多

iOS8中的UIActionSheet添加UIDatePicker后,UIDatePicker不显示问题

解决方法: IOS8以前: UIActionSheet* startsheet = [[UIActionSheet alloc] initWithTitle:title delegate:self cancelButtonTitle:@"确定" destructiveButtonTitle:nil otherButtonTitles:nil, nil]; startsheet.tag = 333; [startsheet addSubview:datePicker]; [startsh