您的位置:首页 > 产品设计 > UI/UE

UIAlertControllr的使用

2016-03-31 10:07 337 查看
今天回顾了一下UIAlertController ios8以前的方法被废弃了 所以就用新的方法 用起来感觉不错 更简洁


使用的步骤

第一步 初始化
+ (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;

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: