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

UIAlertController的用法示例

2015-10-15 12:51 501 查看
在iOS8中 我们熟悉的UIAlertView已经不被苹果提倡了 取而代之的是UIAlertController 关于详细的解释说明上一篇转载的文章里 已经很详细的介绍了 这篇文章 主要来自己敲一下代码 顺便看一下实现的效果有什么不同

// 1.Alert

UIAlertController *controller = [UIAlertControlleralertControllerWithTitle:nilmessage:@"确定要注销吗"preferredStyle:UIAlertControllerStyleAlert];

UIAlertAction *okAction = [UIAlertActionactionWithTitle:@"确定"style:UIAlertActionStyleDestructivehandler:^(UIAlertAction
*_Nonnull action) {

[self.navigationControllerpopToRootViewControllerAnimated:YES];

}];

UIAlertAction *cancleAction = [UIAlertActionactionWithTitle:@"取消"style:UIAlertActionStyleCancelhandler:nil];

[controller addAction:okAction];

[controller addAction:cancleAction];

[selfpresentViewController:controller
animated:YEScompletion:nil];

实现的效果是这样的:


// 2.ActionSheet

UIAlertController *controller = [UIAlertControlleralertControllerWithTitle:@"Tips"message:@"确定要注销吗"preferredStyle:UIAlertControllerStyleActionSheet];

UIAlertAction *okAction = [UIAlertActionactionWithTitle:@"确定"style:UIAlertActionStyleDestructivehandler:^(UIAlertAction
*_Nonnull action) {

[self.navigationControllerpopToRootViewControllerAnimated:YES];

}];

UIAlertAction *cancleAction = [UIAlertActionactionWithTitle:@"取消"style:UIAlertActionStyleCancelhandler:nil];

[controller addAction:okAction];

[controller addAction:cancleAction];

[selfpresentViewController:controller
animated:YEScompletion:nil];

实现效果是这样的:


//3.textfield

#pragma 注意
如果想加入textfield
类型只能是StyleAlert

UIAlertController *controller = [UIAlertControlleralertControllerWithTitle:@"Tips"message:@"请输入用户名密码"preferredStyle:UIAlertControllerStyleAlert];

[controller addTextFieldWithConfigurationHandler:^(UITextField *_Nonnull
textField) {

textField.placeholder =
@"用户名";

}];

[controller addTextFieldWithConfigurationHandler:^(UITextField *_Nonnull
textField) {

textField.placeholder =
@"密码";

textField.secureTextEntry =
YES;

}];

UIAlertAction *okAction = [UIAlertActionactionWithTitle:@"确定"style:UIAlertActionStyleDestructivehandler:^(UIAlertAction
*_Nonnull action) {

[self.navigationControllerpopToRootViewControllerAnimated:YES];

}];

UIAlertAction *cancelAction = [UIAlertActionactionWithTitle:@"取消"style:UIAlertActionStyleCancelhandler:nil];

[controller addAction:okAction];

[controller addAction:cancelAction];

[selfpresentViewController:controller
animated:YEScompletion:nil];
实现效果是这样的:


最后加上对textfield用户名长度判断处理的代码

- (IBAction)logout:(UIBarButtonItem *)sender {

#pragma 注意
如果想加入textfield
类型只能是StyleAlert

controller = [UIAlertController
alertControllerWithTitle:@"Tips"
message:@"请输入用户名密码"
preferredStyle:UIAlertControllerStyleAlert];

/*增加一个监听*/

[controller
addTextFieldWithConfigurationHandler:^(UITextField *
_Nonnull textField) {

textField.placeholder =
@"用户名(长度至少为3位)";

[[NSNotificationCenter
defaultCenter]addObserver:self
selector:@selector(usernameChanged)
name:UITextFieldTextDidChangeNotification
object:textField];

}];

[controller
addTextFieldWithConfigurationHandler:^(UITextField *
_Nonnull textField) {

textField.placeholder =
@"密码";

textField.secureTextEntry =
YES;

}];

/*当确定按钮按下时
读取*/

okAction = [UIAlertAction
actionWithTitle:@"确定"
style:UIAlertActionStyleDestructive
handler:^(UIAlertAction *
_Nonnull action) {

[[NSNotificationCenter
defaultCenter]removeObserver:self];

}];

UIAlertAction *cancelAction = [UIAlertAction
actionWithTitle:@"取消"
style:UIAlertActionStyleCancel
handler:^(UIAlertAction *
_Nonnull action) {

[self.navigationController
popToRootViewControllerAnimated:YES];

}];

[controller
addAction:okAction];

[controller addAction:cancelAction];

okAction.enabled =
NO;

[self
presentViewController:controller
animated:YES
completion:nil];

}

- (void)usernameChanged{

okAction.enabled =
controller.textFields.firstObject.text.length >=
3;

}

实现效果是这样的:

长度没有3位:



如果长度达到了3位 显示效果是这样的
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: