UIAlertAction添加输入框
2016-05-04 13:43
561 查看
* UIAlertController & UIAlertAction
* 1. 在iOS8中,我们失去了两个非常简单的控件,那就是UIAlertView、UIActionSheet,取而代之的是UIAlertController和UIAlertAction
* 2. 在iOS8中,UIAlertController控件使用两种样式代理题UIAlertView和UIActionSheet
* 3. 意想不到的事情是UIAlertController的父类是UIViewController,所以要想显示出来不再是之前的show方法,而是使用推出模态视图的方式推出
* 4. 在UIAlertController中,并没有按钮的添加,有的是Title和Message这样的信息显示,而按钮,则是放在UIAlertAction类中了
* 5. 如果我们想要添加一个按钮,通过创建UIAlertAction对象进行添加,按钮的事件也不再使用代理方法,而是通过Block的方式,我们可以添加多个按钮
* 6. 除此之外,UIAlertController也可以添加UITextField,UITextField的设置放在了Block内部,我们可以通过数组alertController.textFields来获取添加的输入框中的值,同样,输入框我们可以添加多个
* !!!7. !!!注意:输入框的添加只能在alertView样式下添加,如果是在actionSheet方式下添加,会造成运行时错误,所以在添加输入框前最好添加判断
*/
// 看下效果图
// 主要代码
#pragma mark 分段控制器事件
- (IBAction)segmentedControlAction:(UISegmentedControl *)sender
{
// 1. 创建UIAlertControl变量,但并不穿GIAn
UIAlertController *alertController = nil;
// 2. 根据点击的item创建不同样式的alertController
switch (sender.selectedSegmentIndex) {
case 0: { // 弹出AlertView
alertController = [UIAlertController alertControllerWithTitle:@"Title" message:@"Message" preferredStyle:UIAlertControllerStyleAlert];
break;
}
case 1: { // 弹出ActionSheet
alertController = [UIAlertController alertControllerWithTitle:@"Title" message:@"Message" preferredStyle:UIAlertControllerStyleActionSheet];
break;
}
default:
break;
}
// 3. 添加取消按钮
// 3.1 UIAlertAction 表示一个按钮,同时,这个按钮带有处理事件的block
UIAlertAction *action = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
NSLog(@"取消");
}];
// 3.2 添加到alertController上
[alertController addAction:action];
// 4. 添加需要谨慎操作的按钮,文字默认是红色的
[alertController addAction:({
UIAlertAction *action = [UIAlertAction actionWithTitle:@"谨慎操作的按钮" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
NSLog(@"谨慎操作的按钮");
}];
action;
})];
// 5. 添加确定按钮
[alertController addAction:({
UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
NSLog(@"确定");
// 打印输入框的用户名和密码
NSString *userNameStr = [alertController.textFields[0] text];
NSString *passwordStr = [alertController.textFields[1] text];
NSLog(@"userName is: %@ password is: %@", userNameStr, passwordStr);
}];
action;
})];
// 6. 添加输入框到alertView中,注意,actionSheet是没有办法添加textField的,强行添加会Crash
if (alertController.preferredStyle == UIAlertControllerStyleAlert) {
// 添加用户名输入框
[alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
// 给输入框设置一些信息
textField.placeholder = @"请输入用户名";
textField.textAlignment = NSTextAlignmentCenter;
}];
// 添加密码输入框
[alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
textField.placeholder = @"请输入密码";
textField.secureTextEntry = YES;
textField.textAlignment = NSTextAlignmentCenter;
}];
}
// 7. 显示(使用模态视图推出)
[self presentViewController:alertController animated:YES completion:nil];
}
二。在AlertController中直接添加输入框
* 1. 在iOS8中,我们失去了两个非常简单的控件,那就是UIAlertView、UIActionSheet,取而代之的是UIAlertController和UIAlertAction
* 2. 在iOS8中,UIAlertController控件使用两种样式代理题UIAlertView和UIActionSheet
* 3. 意想不到的事情是UIAlertController的父类是UIViewController,所以要想显示出来不再是之前的show方法,而是使用推出模态视图的方式推出
* 4. 在UIAlertController中,并没有按钮的添加,有的是Title和Message这样的信息显示,而按钮,则是放在UIAlertAction类中了
* 5. 如果我们想要添加一个按钮,通过创建UIAlertAction对象进行添加,按钮的事件也不再使用代理方法,而是通过Block的方式,我们可以添加多个按钮
* 6. 除此之外,UIAlertController也可以添加UITextField,UITextField的设置放在了Block内部,我们可以通过数组alertController.textFields来获取添加的输入框中的值,同样,输入框我们可以添加多个
* !!!7. !!!注意:输入框的添加只能在alertView样式下添加,如果是在actionSheet方式下添加,会造成运行时错误,所以在添加输入框前最好添加判断
*/
// 看下效果图
// 主要代码
#pragma mark 分段控制器事件
- (IBAction)segmentedControlAction:(UISegmentedControl *)sender
{
// 1. 创建UIAlertControl变量,但并不穿GIAn
UIAlertController *alertController = nil;
// 2. 根据点击的item创建不同样式的alertController
switch (sender.selectedSegmentIndex) {
case 0: { // 弹出AlertView
alertController = [UIAlertController alertControllerWithTitle:@"Title" message:@"Message" preferredStyle:UIAlertControllerStyleAlert];
break;
}
case 1: { // 弹出ActionSheet
alertController = [UIAlertController alertControllerWithTitle:@"Title" message:@"Message" preferredStyle:UIAlertControllerStyleActionSheet];
break;
}
default:
break;
}
// 3. 添加取消按钮
// 3.1 UIAlertAction 表示一个按钮,同时,这个按钮带有处理事件的block
UIAlertAction *action = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
NSLog(@"取消");
}];
// 3.2 添加到alertController上
[alertController addAction:action];
// 4. 添加需要谨慎操作的按钮,文字默认是红色的
[alertController addAction:({
UIAlertAction *action = [UIAlertAction actionWithTitle:@"谨慎操作的按钮" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
NSLog(@"谨慎操作的按钮");
}];
action;
})];
// 5. 添加确定按钮
[alertController addAction:({
UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
NSLog(@"确定");
// 打印输入框的用户名和密码
NSString *userNameStr = [alertController.textFields[0] text];
NSString *passwordStr = [alertController.textFields[1] text];
NSLog(@"userName is: %@ password is: %@", userNameStr, passwordStr);
}];
action;
})];
// 6. 添加输入框到alertView中,注意,actionSheet是没有办法添加textField的,强行添加会Crash
if (alertController.preferredStyle == UIAlertControllerStyleAlert) {
// 添加用户名输入框
[alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
// 给输入框设置一些信息
textField.placeholder = @"请输入用户名";
textField.textAlignment = NSTextAlignmentCenter;
}];
// 添加密码输入框
[alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
textField.placeholder = @"请输入密码";
textField.secureTextEntry = YES;
textField.textAlignment = NSTextAlignmentCenter;
}];
}
// 7. 显示(使用模态视图推出)
[self presentViewController:alertController animated:YES completion:nil];
}
二。在AlertController中直接添加输入框
相关文章推荐
- UIPickerView 选择控制器----ios控件 UIPickerView应用 制作简易省、市、地区选择器
- 53. Maximum Subarray My Submissions QuestionEditorial Solution
- XMG 调用UIWindow hitTest方法
- 【Arduino官方教程第一辑】示例程序 2-9 制作你的特雷门琴
- 学习Handler、MessageQueue、Looper及其之间的关系
- java String、StringBuffer、StringBuilder
- 90、 Android UI模板设计
- 本地推送UILocalNOtification整理
- String、StringBuilder、StringBuffer的区别
- 非等高cell,根据图片宽高,排布tableview
- UITableViewCell高度自适应探索--UITableView+FDTemplateLayoutCell(转载)
- Android属性之build.prop生成过程分析
- UIViewContentMode
- 基于HBase的消息队列 HQueue
- 亲测UISearchBar Delegate的11个方法和UISearchDisplay Delegate的12个方法的调用顺序
- ExtJS -- QuickTip TextField鼠标移过提示内容
- String、StringBuffer与StringBuilder之间区别
- 简单的request爬取
- require.js入门教程
- Leetcode 347 Top K Frequent Elements