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. FZU 1914 Funny Positive Sequence(线性算法)

    这个当时我没有做出来,看了很多人包括学长的代码才懂,我感觉最好的方法还是下面那一种,标记以谁开头的是不行的,我感觉有点不好理解,如果不懂举组样例在纸上写一下就会比较清楚了 #include<io ...

  2. php 创建删除数据库

    <?php $dbhost = 'localhost:3306'; $dbuser = 'root'; $dbpass = ''; $conn = mysql_connect($dbhost, ...

  3. PHP资源类型

    在PHP中,我们经常使用到资源类型变量.例如:mysql连接.文件句柄等. 这些变量无法使用标量来表示,那么在Zend内核中是如何将PHP中的资源变量与C语言中的资源衔接的呢? 一.资源变量在PHP中 ...

  4. PHP递归算法的一个实例 帮助理解

    递归函数为自调用函数,在函数体内直接或间接自己调用自己,但需要设置自调用的条件,若满足条件,则调用函数本身,若不满足则终止本函数的自调用,然后把目前流程的主控权交回给上一层函数来执行,可能这样给大家讲 ...

  5. 一个appium 博客

    http://www.cnblogs.com/tobecrazy/category/699177.html appium Java控制Appium server start/stop 摘要: 相信很多 ...

  6. Textbox服务器控件

    <body> <form id="form1" runat="server"> <div> 姓名:<asp:TextB ...

  7. zf-关于业务量图表没有出现统计柱形图问题

    是实现类里的 objs[0] = ht.get("BUS_NAME");objs[1] = ht.get("NUM"); 这个写成小写了 改成大写即可,原来这个 ...

  8. java中把list列表转为arrayList以及arraylist数组截取的简单方法

    java中把list列表转为arrayList以及arraylist数组截取的简单方法 package xiaobai; import java.util.ArrayList; import java ...

  9. div定位

    1.float定位带来的问题<html> <head> <title>div浮动引发的问题</title> </head> <styl ...

  10. FIFO 和 LRU 调度算法

    在一个采用页式虚拟存储管理的系统中(字地址序列.页号.块号均从零开始编址),有一用户作业,它依次要访问的字地址序列是:15,128,300,388,246,402,223,142,360,267,若该 ...