IOS8新特性之——UIAlertController的使用

iOS新特性主要体现在UI方面,也是需要掌握的,苹果希望开发人员使用统一的方法,对各种型号的都能使用。首先体现在以下几个方面:

1. UIAlertController

2. UIPresentationController

3. UIPopoverPresentationController

(以前的UIPopoverView只运行在Ipad上,如果运行在iPhone上是会崩溃的)

4. Sizeclass+Autolayout 进行屏幕适配 sizeClassiOS8新出的)

5. App Extension 应用拓展。(下拉屏幕后随意拖拽组件)

6. Core Image iOS 5.0开始就有了,滤镜,美图秀秀就是用的此知识)

7. ………

UIAlertViewController的使用:

新建工程。


设置风格为文本输入框的风格或密码输入框的风格。


设置输入框的风格为登陆输入框的格式。


如果想获取文本框——UITextField *field = [alert textFieldAtIndex:0];

对于UIAlertView 就只能有三种格式的输入框,而UIAlertController可以有任意多个输入框。

UIActionSheet的使用


UIAlertController的使用

查看文档, 定义如下,说明UIAlertController是继承于UIViewController的控制器。


UIAlertController出来之前,需要遵循代理,实现代理方法,要根据按钮的索引进行判断进行事件处理。使用UIAlertController之后每一个按钮都对应一个UIAction,按钮的事件只需要在bolck参数内实现即可。

如果有两个按钮就左右排列,如果有三个按钮就上下排列。


三个按钮


添加输入框的格式如下:


() 如果要获取输入框的值有两种方法:

方法一:声明一个全局的输入框属性,在输入框的actionBlock参数内,将textField赋给全局变量。

方法二:利用alertController自带的textFields数组属性,进行获取。

(二)另外需要注意的是:UIAlertControllerStyleAlert这种格式是针对弹窗而言的,如果换成ActionSheet的格式是不能添加文本输入框的否则会报错崩溃。

(三)避免循环引用,使用弱引用声明alert

(四)监听文本框的代理。有两种方式:一:通过设置代理 textField.delegate = self;进行处理。

二:通过监听通知进行处理。注意:通知方式虽然能监听文本框内容的改变,但是并不能获取里面的内容。另外,UIAlertController是一个控制器在点击按钮的时候销毁,所以还要在点击每个按钮的Block块中移除通知。不建议使用这一种。

(五)因为UITextField继承于UIControl,我们可以通过addTarget方法实现监听文字改变。UITextField继承于UIControl,打开如下:


具体代码如下:

<span style="font-size:18px;">//
//  ViewController.m
//  01.UIAlertController
//
//  Created by 刘刘勋 on 16/1/23.
//  Copyright © 2016年 aaaaa. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController
/*
 如果要对弹窗上按钮的事件进行处理,就要当前的控制器遵循代理,并实现其代理方法
 */

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{

    //  1.UIAlertView
    // 2. UIActionSheet
    // ios8 开始:UIAlertController == UIActionview + UIActionSheet
    /*
   (一) 如果要获取输入框的值有两种方法:
     方法一:声明一个全局的输入框属性,在输入框的action的Block参数内,将textField赋给全局变量。
     方法二:利用alertController自带的textFields数组属性,进行获取。
      
  (二)另外需要注意的是:UIAlertControllerStyleAlert 这种格式是针对弹窗而言的,如果换成ActionSheet的格式是不能添加文本输入框的否则会报错崩溃。
  (三)避免循环引用,使用弱引用声明alert
  (四)监听文本框的代理。有两种方式:一:通过设置代理 textField.delegate = self;进行处理。
     二:通过监听通知进行处理。注意:通知方式虽然能监听文本框内容的改变,但是并不能获取里面的内容。另外,UIAlertController是一个控制器在点击按钮的时候销毁,所以还要在点击每个按钮的Block块中移除通知。不建议使用这一种。
 (五)因为UITextField继承于UIControl,我们可以通过addTarget方法实现监听文字改变。
     */
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"警告" message:@"你患有精神病请治疗" preferredStyle:UIAlertControllerStyleAlert];
    
    // 添加文本框
    [alert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
        textField.textColor = [UIColor redColor];
        textField.text = @"123456";
      //  textField.delegate =self;
      //  [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(usernameDidChange:) name:UITextFieldTextDidChangeNotification object:textField];
        [textField addTarget:self action:@selector(usernameDidChange:) forControlEvents:UIControlEventEditingChanged];
        
    }];
    
    [alert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
        textField.textColor = [UIColor redColor];
        textField.secureTextEntry = YES;
        textField.text = @"abcdefg";
    }];
    

    // 添加按钮
    UIAlertAction *action = [UIAlertAction actionWithTitle:@"警告" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"点击了警告按钮");
    }];
    [alert addAction:action];

    [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"点击了取消按钮");
    }]];
    // 为了避免循环引用,需要将alert声明为弱引用
    __weak typeof(alert) weakAlert = alert;
    [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"点击了确定按钮: %@----%@",weakAlert.textFields.firstObject.text,weakAlert.textFields.lastObject.text);
    }]];
    [alert addAction:[UIAlertAction actionWithTitle:@"其他" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"点击了其他按钮");
    }]];
    
    // 以后想显示控制器,就用modom进行显示 就是控制器跳转 模态窗口进行显示
    [self presentViewController:alert animated:YES completion:nil];

    
}

//-(void)usernameDidChange:(NSNotification *)notifiction{
// 
//    NSLog(@"%@",notifiction.userInfo);
//
//}


-(void)usernameDidChange:(UITextField *)textField{
    
    NSLog(@"%@",textField.text);
    
}

// actionSheet的使用
-(void)actionSheet{
    // destructiveButtonTitle :摧毁性的按钮表示确定执行修改的操作 例如确定删除等等
    UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@"警告" delegate:nil cancelButtonTitle:@"取消" destructiveButtonTitle:@"确定" otherButtonTitles:@"关闭", nil];
    [sheet showInView:self.view];
}

// alertView的使用
-(void)alertView{
    // 危险操作:弹窗提醒
    //  1.UIAlertView
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"警告" message:@"你患有精神病请治疗" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    // alert.alertViewStyle = UIAlertViewStylePlainTextInput;
    // alert.alertViewStyle = UIAlertViewStyleSecureTextInput;
    alert.alertViewStyle = UIAlertViewStyleLoginAndPasswordInput;
    
    // 如果想获取弹窗上的文本框
    UITextField *field = [alert textFieldAtIndex:0];
    [alert show];
}
@end
</span>
运行结果如下:



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值