UIAlertController(警告栏) 自学之初体验
UIAlertController有两种样式 preferredStyle:
UIAlertControllerStyleAlert (位于屏幕的中部)
UIAlertControllerStyleActionSheet(位于屏幕的下方)
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"标题" message:@"默罕默德~本拉登" preferredStyle:UIAlertControllerStyleAlert]; //UIAlertController的创建
UIAlertAction是UIAlertController的按钮样式
title按钮的名称,style按钮的样式,handler处理层序(点击按钮执行的代码)
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消cancel" style:UIAlertActionStyleCancel handler:nil];
UIAlertAction *defaultAction = [UIAlertAction actionWithTitle:@"默认" style:UIAlertActionStyleDefault handler:nil];
UIAlertAction *resetAction = [UIAlertAction actionWithTitle:@"重置" style:UIAlertActionStyleDestructive handler:nil];
//添加按钮到UIAlertController上
[alert addAction:cancelAction];
[alert addAction:defaultAction];
[alert addAction:resetAction];
1、文本输入框只能添加到Alert的风格中,ActionSheet是不允许的;
2、UIAlertController具有只读属性的textFields数组,需要可直接按自己需要的顺序添加;
3、添加方式是使用block,参数是UITextField;
4、添加UITextField监听方法和实现方法。
//添加文本输入框
[alert addTextFieldWithConfigurationHandler:^(UITextField *textField){
textField.placeholder = @"登陆";
//可以为textField添加事件
}];
[alert addTextFieldWithConfigurationHandler:^(UITextField *textField){
textField.placeholder = @"密码";
textField.secureTextEntry = YES;
//可以为textField添加事件
}];
添加一个事件可以用来输出用户名和密码(textFields是属性,是一个数组)
UIAlertAction *getAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){
UITextField *login = alert.textFields[0];
UITextField *passWord = alert.textFields[1];
NSLog(@"登陆:%@ 密码:%@",login.text,passWord.text);
}];//获取textField的文本内容
[alert addAction:getAction];
如果要监听textField开始,结束,改变状态,需要添加监听代码
[alert addTextFieldWithConfigurationHandler:^(UITextField *textField){
textField.placeholder = @"添加监听代码";
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(alertTextFieldDidChange:) name:UITextFieldTextDidChangeNotification object:textField];
}];
[self presentViewController:alert animated:YES completion:^{
}]; //模态推送到页面上
//监听的方法
-(void)alertTextFieldDidChange:(NSNotification *)notification{
UIAlertController *alertController = (UIAlertController *)self.presentedViewController;
if (alertController) {
//为textFields数组中下标为2的textField为监听对象
//也是alertController.textFields.lastObject
UITextField *listen = alertController.textFields[2];
//限制,如果listen限制输入长度在5个字符内,否则不允许点击默认Defult键
//当UITextField输入字数超过5个,按钮变灰色,enable为NO
UIAlertAction *action = alertController.actions.lastObject;
action.enabled = listen.text.length<=5;
}
UIAlertController(警告栏) 自学之初体验的更多相关文章
- UIPickerView(选择控制器) 自学之初体验
UIPickerView 是一个选择器控件, 它可以生成单列的选择器,也可生成多列的选择器,而且开发者完全可以自定义选择项的外观,因此用法非常灵活.UIPickerView 直接继承了 UIView ...
- iOS7 初体验
iOS7 初体验 近日来由于iOS7的发布,引来业界的各种吐槽. 为了体验一把,我已经把iPhone5刷成了iOS7,也下载Xcode5-DP并进行了测试.我想说的是iOS7与Xcode5-DP中新增 ...
- 在同一个硬盘上安装多个 Linux 发行版及 Fedora 21 、Fedora 22 初体验
在同一个硬盘上安装多个 Linux 发行版 以前对多个 Linux 发行版的折腾主要是在虚拟机上完成.我的桌面电脑性能比较强大,玩玩虚拟机没啥问题,但是笔记本电脑就不行了.要在我的笔记本电脑上折腾多个 ...
- python--爬虫入门(七)urllib库初体验以及中文编码问题的探讨
python系列均基于python3.4环境 ---------@_@? --------------------------------------------------------------- ...
- iOS7初体验(3)——图像资源Images Assets
开始之前,首先回顾一下iOS7初体验(1)——第一个应用程序HelloWorld中的一张图,如下所示: 本文便分享一下Images.xcassets的体验~_~ 1. 打开此前使用过的HelloWor ...
- OPhone SDK初体验
OPhone SDK初体验 write by 九天雁翎(JTianLing) -- blog.csdn.net/vagrxie 讨论新闻组及文件 背景说明 中国伟大的垄断龙头,世界上也是顶尖的中移动最 ...
- Git 使用初体验
http://my.oschina.net/moooofly/blog/228608 很久之前在 http://git.oschina.net/ 上创建了一个私有项目 modb ,目的主要是用来学习如 ...
- vue.js2.0 自定义组件初体验
理解 组件(Component)是 Vue.js 最强大的功能之一.组件可以扩展 HTML 元素,封装可重用的代码.在较高层面上,组件是自定义元素, Vue.js 的编译器为它添加特殊功能.在有些情况 ...
- PHP初体验
PHP初体验 提笔写初体验总不知道从何说起,直接聊PHP中的函数.PHP网络技术.数据库操作.PHP模板等感觉又不是初体验.最后还是决定从PHP的面向对象.PHP的魔术方法.PHP的反射.PHP中的异 ...
随机推荐
- oc随笔六:字典
#import <Foundation/Foundation.h> int main(int argc, const char * argv[]) { @autoreleasepool { ...
- CUICatalog: Invalid asset name supplied:
[UIImage imageNamed:name];但是这个name却是空的,所以就报了这个错了. 解决方法,在项目中搜索UIImage imageNamed:,然后打印看看所谓的name是否为空.找 ...
- Java的一点内容(2)
1 面向对象的三个原则 封装性 封装的基本单元是类(class),类是一个抽象的逻辑结构,而类的对象是一个真实的物理实体:类的目的是封装复杂性,在类内部存在隐藏实现复杂性机制: 封装(encapsul ...
- paip.输入法编程----一级汉字1000个
paip.输入法编程----一级汉字1000个.txt 作者Attilax , EMAIL:1466519819@qq.com 来源:attilax的专栏 地址:http://blog.csdn. ...
- linux下查找文件和文件内容
find /xxx -name "*" | xargs grep "某内容" /xxx表示路径,"*"表示在含有某关键字名字下的文件中查找, ...
- SQl 判断 表 视图 临时表等 是否存在
1.判断是否存在addOneArticle这个存储过程 if Exists(select name from sysobjects where NAME = 'addOneArticle' and t ...
- 用委托、匿名函数、Lambda的方式输出符合要求的数
最近看了一些博客,对委托和匿名函数和Lambda的方式有了一些更深的理解,在前人的基础上.我也写3个例子 using System; using System.Collections.Generic; ...
- [POJ] 2456 Aggressive cows (二分查找)
题目地址:http://poj.org/problem?id=2456 最大化最小值问题.二分牛之间的间距,然后验证. #include<cstdio> #include<iostr ...
- Qt之四方分割器QuadSplitter
在Qt经常会用到分割器QSplitter,可以对多个控件进行水平或者垂直分割,但有一些特殊的需求无法满足,比如:四方分割...QuadSplitter是qt-apps里面的一个应用,挺不错的,拿来和大 ...
- Visual Assist X在Windows 8.1下出现中文乱码的解决方法
这主要是输入法造成的,我的输入法中有US.中文.搜狗输入法三个输入法:通过搜狗输入法管理器把“中文”去掉,或者通过语言首选项把“中文”去掉就不会在出现乱码. 这个办法的思路来自于http://www. ...
