iOS开发——UIAlertController
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的更多相关文章
- iOS开发系列--通讯录、蓝牙、内购、GameCenter、iCloud、Passbook系统服务开发汇总
--系统应用与系统服务 iOS开发过程中有时候难免会使用iOS内置的一些应用软件和服务,例如QQ通讯录.微信电话本会使用iOS的通讯录,一些第三方软件会在应用内发送短信等.今天将和大家一起学习如何使用 ...
- IOS开发基础知识--碎片50
1:Masonry 2个或2个以上的控件等间隔排序 /** * 多个控件固定间隔的等间隔排列,变化的是控件的长度或者宽度值 * * @param axisType 轴线方向 * @param fi ...
- iOS开发中的4种数据持久化方式【二、数据库 SQLite3、Core Data 的运用】
在上文,我们介绍了ios开发中的其中2种数据持久化方式:属性列表.归档解档.本节将继续介绍另外2种iOS持久化数据的方法:数据库 SQLite3.Core Data 的运 ...
- iOS开发系列通讯录、蓝牙、内购、GameCenter、iCloud、Passbook系统服务开
--系统应用与系统服务 iOS开发过程中有时候难免会使用iOS内置的一些应用软件和服务,例如QQ通讯录.微信电话本会使用iOS的通讯录,一些第三方软件会在应用内发送短信等.今天将和大家一起学习如何使用 ...
- iOS:iOS开发非常全的三方库、插件等等
iOS开发非常全的三方库.插件等等 github排名:https://github.com/trending, github搜索:https://github.com/search. 此文章转自git ...
- iOS开发--iOS及Mac开源项目和学习资料
文/零距离仰望星空(简书作者)原文链接:http://www.jianshu.com/p/f6cdbc8192ba著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”. 原文出处:codecl ...
- iOS开发——UI篇&提示效果
提示效果 关于iOS开发提示效果是一个很常见的技术,比如我们平时点击一个按钮,实现回馈,或者发送网络请求的时候! 技术点: 一:View UIAlertView UIActionSheet 二:控制器 ...
- iOS开发技巧系列---使用链式编程和Block来实现UIAlertView
UIAlertView是iOS开发过程中最常用的控件之一,是提醒用户做出选择最主要的工具.在iOS8及后来的系统中,苹果更推荐使用UIAlertController来代替UIAlertView.所以本 ...
- IOS开发-OC学习-常用功能代码片段整理
IOS开发-OC学习-常用功能代码片段整理 IOS开发中会频繁用到一些代码段,用来实现一些固定的功能.比如在文本框中输入完后要让键盘收回,这个需要用一个简单的让文本框失去第一响应者的身份来完成.或者是 ...
随机推荐
- cell reuse & disposebag
For my project I've made base cell class TableViewCell: UITableViewCell { private(set) var disposeBa ...
- html5利用websocket完成的推送功能
利用websocket和java完成的消息推送功能,服务器用的是tomcat7.0,一些东西是自己琢磨的,也不知道恰不恰当,不恰当处,还请各位见谅,并指出. 程序简单来说,就是客户A可以发送消息给客户 ...
- 关于在jsp中的表达式
列子: <%List<F_dd_tourist_info_markup> tourists = (List<F_dd_tourist_info_markup>) requ ...
- 隐式intent启动电子邮件,不需要非电子邮件应用程序。
Intent intent = new Intent(Intent.ACTION_SENDTO); intent.setType("text/plain"); intent.put ...
- CodeForces 689C Mike and Chocolate Thieves (二分最大化最小值)
题目并不难,就是比赛的时候没敢去二分,也算是一个告诫,应该敢于思考…… #include<stdio.h> #include<iostream> using namespace ...
- linux的mount(挂载)NFS 共享,命令详解
Linux下挂载(mount)光盘镜像文件.移动硬盘.U盘.Windows和NFS网络共享 linux是一个优秀的开放源码的操作系统,可以运行在大到巨型小到掌上型各类计算机系统上,随着 linux系统 ...
- GridView规则显示图片
首先是自定义的GridVIew,ListView嵌套GridVIew时必须自定义GridVIew,不嵌套时可以不用.这里的GridView会根据图片多少自动调节大小 //自定义的GridView ,以 ...
- 团队开发里频繁使用 git rebase 来保持树的整洁好吗?
用了以后, 树可以非常清晰, 某种程度上便于追踪, 但是 push --force 就多多了,不用呢, 合并没有远程仓库被修改的麻烦, 可是追踪又不清晰... git rebase是对commit h ...
- Django: 配置和静态文件
运行django-admin.py startproject [project-name] 命令会生成一系列文件,在django 1.6版本以后的settings.py文件中有以下语句: # Buil ...
- 解决phpmyadmin上传文件大小限制的配置方法(转)
phpmyadmin导入SQL文件时涉及到phpmyadmin上传文件大小限制问题,默认phpmyadmin上传文件大小为2M,如果想要 phpmyadmin上传超过2M大文件,就需要修改phpmya ...