本文转载至 http://blog.csdn.net/liuwuguigui/article/details/39494597
 
 
 

iOS8推出了几个新的“controller”,主要是把类似之前的UIAlertView变成了UIAlertController,这不经意的改变,貌似把我之前理解的“controller”一下子推翻了~但是也无所谓,有新东西不怕,学会使用了就行。接下来会探讨一下这些个新的Controller。

- (void)showOkayCancelAlert {
NSString *title = NSLocalizedString(@"A Short Title Is Best", nil);
NSString *message = NSLocalizedString(@"A message should be a short, complete sentence.", nil);
NSString *cancelButtonTitle = NSLocalizedString(@"Cancel", nil);
NSString *otherButtonTitle = NSLocalizedString(@"OK", nil); UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert]; // Create the actions.
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:cancelButtonTitle style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
NSLog(@"The \"Okay/Cancel\" alert's cancel action occured.");
}]; UIAlertAction *otherAction = [UIAlertAction actionWithTitle:otherButtonTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
NSLog(@"The \"Okay/Cancel\" alert's other action occured.");
}]; // Add the actions.
[alertController addAction:cancelAction];
[alertController addAction:otherAction]; [self presentViewController:alertController animated:YES completion:nil];
}

这是最普通的一个alertcontroller,一个取消按钮,一个确定按钮。

新的alertcontroller,其初始化方法也不一样了,按钮响应方法绑定使用了block方式,有利有弊。需要注意的是不要因为block导致了引用循环,记得使用__weak,尤其是使用到self。

上面的界面如下:

如果UIAlertAction *otherAction这种otherAction多几个的话,它会自动排列成如下:

另外,很多时候,我们需要在alertcontroller中添加一个输入框,例如输入密码:

这时候可以添加如下代码:

 [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
// 可以在这里对textfield进行定制,例如改变背景色
textField.backgroundColor = [UIColor orangeColor];
}];

而改变背景色会这样:

完整的密码输入:

- (void)showSecureTextEntryAlert {
NSString *title = NSLocalizedString(@"A Short Title Is Best", nil);
NSString *message = NSLocalizedString(@"A message should be a short, complete sentence.", nil);
NSString *cancelButtonTitle = NSLocalizedString(@"Cancel", nil);
NSString *otherButtonTitle = NSLocalizedString(@"OK", nil); UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert]; // Add the text field for the secure text entry.
[alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
// Listen for changes to the text field's text so that we can toggle the current
// action's enabled property based on whether the user has entered a sufficiently
// secure entry.
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleTextFieldTextDidChangeNotification:) name:UITextFieldTextDidChangeNotification object:textField]; textField.secureTextEntry = YES;
}]; // Create the actions.
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:cancelButtonTitle style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
NSLog(@"The \"Secure Text Entry\" alert's cancel action occured."); // Stop listening for text changed notifications.
[[NSNotificationCenter defaultCenter] removeObserver:self name:UITextFieldTextDidChangeNotification object:alertController.textFields.firstObject];
}]; UIAlertAction *otherAction = [UIAlertAction actionWithTitle:otherButtonTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
NSLog(@"The \"Secure Text Entry\" alert's other action occured."); // Stop listening for text changed notifications.
[[NSNotificationCenter defaultCenter] removeObserver:self name:UITextFieldTextDidChangeNotification object:alertController.textFields.firstObject];
}]; // The text field initially has no text in the text field, so we'll disable it.
otherAction.enabled = NO; // Hold onto the secure text alert action to toggle the enabled/disabled state when the text changed.
self.secureTextAlertAction = otherAction; // Add the actions.
[alertController addAction:cancelAction];
[alertController addAction:otherAction]; [self presentViewController:alertController animated:YES completion:nil];
}

注意四点:

1.添加通知,监听textfield内容的改变:

// Add the text field for the secure text entry.
[alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
// Listen for changes to the text field's text so that we can toggle the current
// action's enabled property based on whether the user has entered a sufficiently
// secure entry.
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleTextFieldTextDidChangeNotification:) name:UITextFieldTextDidChangeNotification object:textField]; textField.secureTextEntry = YES;
}];

2.初始化时候,禁用“ok”按钮:

otherAction.enabled = NO;

self.secureTextAlertAction = otherAction;//定义一个全局变量来存储

3.当输入超过5个字符时候,使self.secureTextAlertAction = YES:

- (void)handleTextFieldTextDidChangeNotification:(NSNotification *)notification {
UITextField *textField = notification.object; // Enforce a minimum length of >= 5 characters for secure text alerts.
self.secureTextAlertAction.enabled = textField.text.length >= 5;
}

4.在“OK”action中去掉通知:

UIAlertAction *otherAction = [UIAlertAction actionWithTitle:otherButtonTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
NSLog(@"The \"Secure Text Entry\" alert's other action occured."); // Stop listening for text changed notifications.
[[NSNotificationCenter defaultCenter] removeObserver:self name:UITextFieldTextDidChangeNotification object:alertController.textFields.firstObject];
}];

最后是以前经常是alertview与actionsheet结合使用,这里同样也有:

- (void)showOkayCancelActionSheet {
NSString *cancelButtonTitle = NSLocalizedString(@"Cancel", nil);
NSString *destructiveButtonTitle = NSLocalizedString(@"OK", nil); UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet]; // Create the actions.
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:cancelButtonTitle style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
NSLog(@"The \"Okay/Cancel\" alert action sheet's cancel action occured.");
}]; UIAlertAction *destructiveAction = [UIAlertAction actionWithTitle:destructiveButtonTitle style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
NSLog(@"The \"Okay/Cancel\" alert action sheet's destructive action occured.");
}]; // Add the actions.
[alertController addAction:cancelAction];
[alertController addAction:destructiveAction]; [self presentViewController:alertController animated:YES completion:nil];
}

在底部显示如下:

好了,至此,基本就知道这个新的controller到底是怎样使用了。

iOS8中提示框的使用UIAlertController(UIAlertView和UIActionSheet二合一)的更多相关文章

  1. IOS 加载中提示框

    LoadingView.h #import <Foundation/Foundation.h> @class MBProgressHUD; @interface LoadingView : ...

  2. iOS -iOS9中提示框(UIAlertController)的常见使用

    iOS 8 之前提示框主要使用 UIAlertView和UIActionSheet:iOS 9 将UIAlertView和UIActionSheet合二为一为:UIAlertController . ...

  3. echarts中提示框的样式调整

    第一种方法:利用tooltip 里面的配置项 默认就会有写显示 第二种方法:利用formattet回调函数 返回我们想要显示的信息 formatter : function (params) { va ...

  4. js中提示框闪退问题

    当页面存在刷新  或  在线引用iframe框架时(引用框架也会导致刷新) 会导致页面加载时的弹出框闪退 解决方法:设置弹出框在页面或者框架完全加载一段时间后再弹出 <script type=& ...

  5. jquery mobile 请求数据方法执行时显示加载中提示框

    在jquery mobile开发中,经常需要调用ajax方法,异步获取数据,如果异步获取数据方法由于网速等等的原因,会有一个反应时间,如果能在点击按钮后数据处理期间,给一个正在加载的提示,客户体验会更 ...

  6. 微信小程序之----加载中提示框loading

    loading loading只有一个属性hidden .wxml <view> <loading hidden="{{hidden}}"> 加载中... ...

  7. js 提示框的实现---组件开发之(二)

    接着第上一个,在js文件里再增加一个 popModal 模块,实现弹框效果 css 代码: .alert { padding: 15px; margin-bottom: 20px; border: 1 ...

  8. 提示框(UIAlertController)的使用。

    添加出现在屏幕中间的提示框(也就是之前的UIAlertView): UIAlertController * av = [UIAlertController alertControllerWithTit ...

  9. js弹出框、对话框、提示框、弹窗总结

    一.JS的三种最常见的对话框 //====================== JS最常用三种弹出对话框 ======================== //弹出对话框并输出一段提示信息 funct ...

随机推荐

  1. 网络流24题-最长k可重线段集问题

    最长k可重线段集问题 时空限制1000ms / 128MB 题目描述 给定平面 x−O−y 上 n 个开线段组成的集合 I,和一个正整数 k .试设计一个算法,从开线段集合 I 中选取出开线段集合 S ...

  2. PAT 甲级1002 A+B for Polynomials (25)

    1002. A+B for Polynomials (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue T ...

  3. JQuery Option 排序

    <script type="text/javascript"> $(document).ready(function () { $("select" ...

  4. touch下拉刷新

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  5. PatentTips - MPLS Network System

    MPLS (Multi Protocol Label Switching) network system has been watched with keen interest as a techni ...

  6. 快充 IC BQ25896 的 Dynamic Power Management

    Spec 更正: 上面紅色框框應該還要再增加一個 ILIM pin 硬體所設定的 input current limit, 也就是說 input current limit 最多可以從這 3 個 IL ...

  7. http协议相关面试题

    浏览器输入url按回车背后经历了哪些? 1.在PC浏览器的地址栏输入一串URL,然后按Enter键这个页面渲染出来,这个过程中都发生了什么事? 1.首先,在浏览器地址栏中输入url,先解析url,检测 ...

  8. Day 18 函数之一

    函数参数: 1.形参变量只有在被调用时才分配内存单元,在调用结束时,即刻释放所分配的内存单元.因此,形参只在函数内部有效.函数调用结束返回主调用函数后则不能再使用该形参变量 2.实参可以是常量.变量. ...

  9. C#读取Excel 几种方法的体会

    (1) OleDb: 用这种方法读取Excel速度还是非常的快的,但这种方式读取数据的时候不太灵活,不过可以在 DataTable 中对数据进行一些删减修改 这种方式将Excel作为一个数据源,直接用 ...

  10. BZOJ 4568 [Scoi2016]幸运数字(树链剖分 + 异或线性基)

    题目链接  BZOJ 4568 考虑树链剖分+线段树维护每一段区域的异或线性基 对于每个询问,求出该点集的异或线性基.然后求一下这个线性基里面能异或出的最大值即可. #include <bits ...