UIAlertControllr的使用
2016-03-31 10:07
337 查看
今天回顾了一下UIAlertController ios8以前的方法被废弃了 所以就用新的方法 用起来感觉不错 更简洁
第一步 初始化
这里的preferredStyle有两种,sheet和alert
第二步,添加Action(button或者textfield)
添加Button
- (void)addAction:(UIAlertAction *)action
这里的UIAlertAction是一个比较简单的类
style有三种
添加TextField
注意,只能是 UIAlertControllerStyleAlert才能添加Textfield
在block里配置textfield的信息,例如placeholder,background color,secureTextEntry,clearButtonMode,borderStyle
等。
Textfield的保存信息可由UIAlertController的属性Textfields获得。如同上述的例子二一样。
第三步,显示
例如
代码如下:
-(void)showAlertView
{
UIAlertController *alertController = [UIAlertController
alertControllerWithTitle:@"alertController"
message:@"哈哈哈"
preferredStyle:UIAlertControllerStyleAlert];
//账号TextField
[alertController addTextFieldWithConfigurationHandler:^(UITextField *
_Nonnull textField) {
//这里textfield
只是一个block返回的值
要想绑定事件
只能用通知中心了
textField.placeholder =
@"账号";
//添加通知中心
添加观察者 因为不能使用委托
一般用观察者 Observer
模式定义对象间的一对多的关系,当一个对象的状态发生改变的时候,所有依赖他的对象都得到通知并被自动更新
[[NSNotificationCenter
defaultCenter] addObserver:self
selector:@selector(handleTextFieldTextDidChangeNotification:)
name:UITextFieldTextDidChangeNotification
object:textField];
}];
[alertController addTextFieldWithConfigurationHandler:^(UITextField *
_Nonnull textField) {
textField.placeholder =
@"密码";
//文本输入是********
这种模式
textField.secureTextEntry =
YES;
textField.clearButtonMode =
UITextFieldViewModeWhileEditing;
textField.borderStyle =
UITextBorderStyleRoundedRect;
[[NSNotificationCenter
defaultCenter] addObserver:self
selector:@selector(handleTextFieldTextDidChangeNotification:)
name:UITextFieldTextDidChangeNotification
object:textField];
}];
UIAlertAction *cancelAction = [UIAlertAction
actionWithTitle:@"取消"
style: UIAlertActionStyleCancel
handler:^(UIAlertAction *
_Nonnull action) {
NSLog(@"取消后的事件");
//注意block的用法
//移除通知
[[NSNotificationCenter
defaultCenter]
removeObserver:self
name:UITextFieldTextDidChangeNotification
object:alertController.textFields.firstObject];
}];
UIAlertAction *sureAction = [UIAlertAction
actionWithTitle:@"确定"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *
_Nonnull action) {
NSLog(@"确定后的事件");
[[NSNotificationCenter
defaultCenter]
removeObserver:self
name:UITextFieldTextDidChangeNotification
object:alertController.textFields.firstObject];
}];
sureAction.enabled =
NO;
[alertController addAction:cancelAction];
[alertController addAction:sureAction];
//控制器
只能present
出来
[self
presentViewController:alertController animated:YES
completion:nil];
}
#pragma mark 通知
-(void)handleTextFieldTextDidChangeNotification:(NSNotification *)notification
{
//可以用属性来传值 notification
有三个属性
一个name 一个是object
还一个是useinfo
// UITextField *textField = notification.object;
UIAlertController *alertController = (UIAlertController *)self.presentedViewController;
if (alertController) {
UITextField *login = alertController.textFields.lastObject;
UIAlertAction *sureAction = alertController.actions.lastObject;
sureAction.enabled = login.text.length >
6;
}
}
使用的步骤
第一步 初始化+ (instancetype)alertControllerWithTitle:(NSString *)title message:(NSString *)message preferredStyle:(UIAlertControllerStyle)preferredStyle
这里的preferredStyle有两种,sheet和alert
typedef enum UIAlertControllerStyle: NSInteger { UIAlertControllerStyleActionSheet = 0, UIAlertControllerStyleAlert } UIAlertControllerStyle;
第二步,添加Action(button或者textfield)
添加Button
- (void)addAction:(UIAlertAction *)action
这里的UIAlertAction是一个比较简单的类
+ (instancetype)actionWithTitle:(NSString *)title style:(UIAlertActionStyle)style handler:(void (^)(UIAlertAction *action))handler
style有三种
typedef enum UIAlertActionStyle: NSInteger { UIAlertActionStyleDefault = 0,//默认 UIAlertActionStyleCancel,//取消 UIAlertActionStyleDestructive //有可能改变或者数据 } UIAlertActionStyle;
添加TextField
注意,只能是 UIAlertControllerStyleAlert才能添加Textfield
- (void)addTextFieldWithConfigurationHandler:(void (^)(UITextField *textField))configurationHandler
在block里配置textfield的信息,例如placeholder,background color,secureTextEntry,clearButtonMode,borderStyle
等。
Textfield的保存信息可由UIAlertController的属性Textfields获得。如同上述的例子二一样。
第三步,显示
例如
[self presentViewController:alert animated:YES completion:nil];
代码如下:
-(void)showAlertView
{
UIAlertController *alertController = [UIAlertController
alertControllerWithTitle:@"alertController"
message:@"哈哈哈"
preferredStyle:UIAlertControllerStyleAlert];
//账号TextField
[alertController addTextFieldWithConfigurationHandler:^(UITextField *
_Nonnull textField) {
//这里textfield
只是一个block返回的值
要想绑定事件
只能用通知中心了
textField.placeholder =
@"账号";
//添加通知中心
添加观察者 因为不能使用委托
一般用观察者 Observer
模式定义对象间的一对多的关系,当一个对象的状态发生改变的时候,所有依赖他的对象都得到通知并被自动更新
[[NSNotificationCenter
defaultCenter] addObserver:self
selector:@selector(handleTextFieldTextDidChangeNotification:)
name:UITextFieldTextDidChangeNotification
object:textField];
}];
[alertController addTextFieldWithConfigurationHandler:^(UITextField *
_Nonnull textField) {
textField.placeholder =
@"密码";
//文本输入是********
这种模式
textField.secureTextEntry =
YES;
textField.clearButtonMode =
UITextFieldViewModeWhileEditing;
textField.borderStyle =
UITextBorderStyleRoundedRect;
[[NSNotificationCenter
defaultCenter] addObserver:self
selector:@selector(handleTextFieldTextDidChangeNotification:)
name:UITextFieldTextDidChangeNotification
object:textField];
}];
UIAlertAction *cancelAction = [UIAlertAction
actionWithTitle:@"取消"
style: UIAlertActionStyleCancel
handler:^(UIAlertAction *
_Nonnull action) {
NSLog(@"取消后的事件");
//注意block的用法
//移除通知
[[NSNotificationCenter
defaultCenter]
removeObserver:self
name:UITextFieldTextDidChangeNotification
object:alertController.textFields.firstObject];
}];
UIAlertAction *sureAction = [UIAlertAction
actionWithTitle:@"确定"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *
_Nonnull action) {
NSLog(@"确定后的事件");
[[NSNotificationCenter
defaultCenter]
removeObserver:self
name:UITextFieldTextDidChangeNotification
object:alertController.textFields.firstObject];
}];
sureAction.enabled =
NO;
[alertController addAction:cancelAction];
[alertController addAction:sureAction];
//控制器
只能present
出来
[self
presentViewController:alertController animated:YES
completion:nil];
}
#pragma mark 通知
-(void)handleTextFieldTextDidChangeNotification:(NSNotification *)notification
{
//可以用属性来传值 notification
有三个属性
一个name 一个是object
还一个是useinfo
// UITextField *textField = notification.object;
UIAlertController *alertController = (UIAlertController *)self.presentedViewController;
if (alertController) {
UITextField *login = alertController.textFields.lastObject;
UIAlertAction *sureAction = alertController.actions.lastObject;
sureAction.enabled = login.text.length >
6;
}
}
相关文章推荐
- Druid基本配置及内置监控使用
- UITableView滚动到底部(用于评论回复)
- UIImage,CGImage和CGImageRef 区别
- continue 与break 的区别
- IOS学习笔记45--UITableView性能优化
- NSTimer 回调事件被UI交互阻塞的解决方法
- UISplitViewController
- 使用SoapUI 测试Web Service
- UIView的一个弹出动画
- iOS之搜索框UISearchController的使用(iOS8.0以后替代UISearchBar+display)
- 编译错误 --- does not name a type和field `XX' has incomplete type
- ios9中 UIStackView的使用
- CodeForces 632D Longest Subsequence(数论)
- UIAlertView利用runtime传值
- [转] How to change font settings for all UI elements (toolbar and context menus, property editors, etc.)
- 设置UINavigationBar的UI
- IOS开发-UI学习-根据URL显示图片,下载图片的练习(button,textfield,image view,url,data)
- 3分钟看完Build2016 Day 1 Keynote
- 微软Build 2016开发者大会--兑换承诺
- invalid request block size: 21573 (max 4096)...skip错误原因