iOS8之后,UIAlertView和UIActionSheet被干掉了,取而代之的是UIAlertController和UIAlertAction。

  UIAlertController有两种样式,一个是UIAlertControllerStyleAlert,就是以前的UIAlertView,还有一个就是UIAlertControllerStyleActionSheet,也就是以前的UIActionSheet。UIAlertControllerStyleAlert模式中可以加输入框,UIAlertControllerStyleActionSheet不行。为了更像点样子,我设置文本框有输入才能点击确定。

  废话不多说,上代码:

//

//  ViewController.m

//  Demo-hehehe

//

//  Created by yyt on 16/4/21.

//  Copyright © 2016年 yyt. All rights reserved.

//

#import "ViewController.h"

@interface ViewController ()<UIAlertViewDelegate>

@property(nonatomic,strong) UITextField *myTextField;

@property(nonatomic,strong) UIAlertAction *otherAction;

@end

@implementation ViewController

- (void)viewDidLoad {

[super viewDidLoad];

UIButton *alertButton = [UIButton buttonWithType:UIButtonTypeSystem];

alertButton.frame = CGRectMake(40, 140, 200, 40);

alertButton.backgroundColor = [UIColor orangeColor];

[alertButton setTitle:@"AlertButton" forState:UIControlStateNormal];

[alertButton addTarget:self action:@selector(clickAlertButton:) forControlEvents:UIControlEventTouchUpInside];

[self.view addSubview:alertButton];

UIButton *actionSheetButton = [UIButton buttonWithType:UIButtonTypeSystem];

actionSheetButton.frame = CGRectMake(40, 200, 200, 40);

actionSheetButton.backgroundColor = [UIColor orangeColor];

[actionSheetButton setTitle:@"ActionSheetButton" forState:UIControlStateNormal];

[actionSheetButton addTarget:self action:@selector(clickActionSheetButton:) forControlEvents:UIControlEventTouchUpInside];

[self.view addSubview:actionSheetButton];

}

- (void)clickAlertButton:(UIButton*)sender {

UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"AlertButton" message:nil preferredStyle:UIAlertControllerStyleAlert];

UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {

NSLog(@"11111");

}];

[alertController addAction:cancelAction];

UIAlertAction *otherAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {

NSLog(@"222222");

}];

self.otherAction = otherAction;

otherAction.enabled = NO;

[alertController addAction:otherAction];

[alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {

self.myTextField = textField;

textField.placeholder = @"必填";

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

}];

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

}

- (void)handleTextFieldTextDidChangeNotification:(NSNotification *)notification {

if(self.myTextField.text.length > 0) {

self.otherAction.enabled = YES;

} else {

self.otherAction.enabled = NO;

}

}

- (void)clickActionSheetButton:(UIButton*)sender {

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

UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {

NSLog(@"11111");

}];

[alertController addAction:cancelAction];

UIAlertAction *otherAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {

NSLog(@"222222");

}];

[alertController addAction:otherAction];

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

}

@end

iOS开发——UIAlertController的更多相关文章

  1. iOS开发系列--通讯录、蓝牙、内购、GameCenter、iCloud、Passbook系统服务开发汇总

    --系统应用与系统服务 iOS开发过程中有时候难免会使用iOS内置的一些应用软件和服务,例如QQ通讯录.微信电话本会使用iOS的通讯录,一些第三方软件会在应用内发送短信等.今天将和大家一起学习如何使用 ...

  2. IOS开发基础知识--碎片50

      1:Masonry 2个或2个以上的控件等间隔排序 /** * 多个控件固定间隔的等间隔排列,变化的是控件的长度或者宽度值 * * @param axisType 轴线方向 * @param fi ...

  3. iOS开发中的4种数据持久化方式【二、数据库 SQLite3、Core Data 的运用】

                   在上文,我们介绍了ios开发中的其中2种数据持久化方式:属性列表.归档解档.本节将继续介绍另外2种iOS持久化数据的方法:数据库 SQLite3.Core Data 的运 ...

  4. iOS开发系列通讯录、蓝牙、内购、GameCenter、iCloud、Passbook系统服务开

    --系统应用与系统服务 iOS开发过程中有时候难免会使用iOS内置的一些应用软件和服务,例如QQ通讯录.微信电话本会使用iOS的通讯录,一些第三方软件会在应用内发送短信等.今天将和大家一起学习如何使用 ...

  5. iOS:iOS开发非常全的三方库、插件等等

    iOS开发非常全的三方库.插件等等 github排名:https://github.com/trending, github搜索:https://github.com/search. 此文章转自git ...

  6. iOS开发--iOS及Mac开源项目和学习资料

    文/零距离仰望星空(简书作者)原文链接:http://www.jianshu.com/p/f6cdbc8192ba著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”. 原文出处:codecl ...

  7. iOS开发——UI篇&提示效果

    提示效果 关于iOS开发提示效果是一个很常见的技术,比如我们平时点击一个按钮,实现回馈,或者发送网络请求的时候! 技术点: 一:View UIAlertView UIActionSheet 二:控制器 ...

  8. iOS开发技巧系列---使用链式编程和Block来实现UIAlertView

    UIAlertView是iOS开发过程中最常用的控件之一,是提醒用户做出选择最主要的工具.在iOS8及后来的系统中,苹果更推荐使用UIAlertController来代替UIAlertView.所以本 ...

  9. IOS开发-OC学习-常用功能代码片段整理

    IOS开发-OC学习-常用功能代码片段整理 IOS开发中会频繁用到一些代码段,用来实现一些固定的功能.比如在文本框中输入完后要让键盘收回,这个需要用一个简单的让文本框失去第一响应者的身份来完成.或者是 ...

随机推荐

  1. liunx 定时执行 php文件

    which php    寻找php路径

  2. Java中的值栈

    OGNL表示式使用 和 值栈 OGNL是Object Graphic Navigation Language(对象图导航语言)的缩写,它是一个开源项目. Struts2框架使用OGNL作为默认的表达式 ...

  3. windows程序设计(二)

    MFC架构组成 1.CWinApp的派生类 2.必须在全局区定义一个派生类的对象 3.在CWinApp派生类内必须要有InitInstance虚函数的重写函数 在MFC软件工程以App类中的InitI ...

  4. Mysql笔记3数据库基本操作

    1 创建数据库 create database 数据名称 default character set 编码; 2查看常用的编码校验规则 mysql> show character set; 3删 ...

  5. chromium blog

    http://blog.chromium.org/

  6. 理解free命令

    free的输出: total used free shared buffers cached Mem: -/+ buffers/cache: Swap: 第一行: total:总内存 used:已使用 ...

  7. shell输出不换行符合换行符

    输出不换行符 例如 echo "Hello\c" echo " World" //Hello World 输出换行符 echo "username\n ...

  8. Android程序两种退出的方法

    两种程序退出的方法: Context的finish()方法: android.os.Process的killProcess()方法:(当程序isRegistered()失败,说明程序被修改过,调用ki ...

  9. sql server 更新两个表的某个字段

    --临时表 create table tmp_cup ( a varchar(20), b varchar(50), c varchar(20) ) select * from t_customer ...

  10. fragment 数据传递,通信

    Fragment之间的通信   在本节中,你会学到 1.定义接口 2.实现接口 3.将消息传递给fragment 为了重用Fragment UI 组件,在设计中你应该通过定义每一个fragemnt自己 ...