UIAlertController的用法示例
来源:互联网 发布:visual studio python 编辑:程序博客网 时间:2024/05/18 21:39
在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:controlleranimated: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:controlleranimated: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:controlleranimated:YEScompletion:nil];
实现效果是这样的:最后加上对textfield用户名长度判断处理的代码
- (IBAction)logout:(UIBarButtonItem *)sender {
#pragma 注意 如果想加入textfield 类型只能是StyleAlert
controller = [UIAlertControlleralertControllerWithTitle:@"Tips"message:@"请输入用户名密码"preferredStyle:UIAlertControllerStyleAlert];
/*增加一个监听*/
[controlleraddTextFieldWithConfigurationHandler:^(UITextField *_Nonnull textField) {
textField.placeholder = @"用户名(长度至少为3位)";
[[NSNotificationCenter defaultCenter]addObserver:selfselector:@selector(usernameChanged)name:UITextFieldTextDidChangeNotificationobject:textField];
}];
[controlleraddTextFieldWithConfigurationHandler:^(UITextField *_Nonnull textField) {
textField.placeholder = @"密码";
textField.secureTextEntry = YES;
}];
/*当确定按钮按下时 读取*/
okAction = [UIAlertActionactionWithTitle:@"确定"style:UIAlertActionStyleDestructivehandler:^(UIAlertAction *_Nonnull action) {
[[NSNotificationCenterdefaultCenter]removeObserver:self];
}];
UIAlertAction *cancelAction = [UIAlertActionactionWithTitle:@"取消"style:UIAlertActionStyleCancelhandler:^(UIAlertAction *_Nonnull action) {
[self.navigationControllerpopToRootViewControllerAnimated:YES];
}];
[controller addAction:okAction];
[controller addAction:cancelAction];
okAction.enabled =NO;
[selfpresentViewController:controlleranimated:YEScompletion:nil];
}
- (void)usernameChanged{
okAction.enabled =controller.textFields.firstObject.text.length >=3;
}
长度没有3位:
如果长度达到了3位 显示效果是这样的
- UIAlertController的用法示例
- UIAlertController的使用示例
- UIAlertController的用法
- UIAlertController的用法
- UIAlertController的用法
- UIAlertController的用法
- UIAlertController的用法
- UIAlertController的用法
- UIAlertController的用法
- UIAlertController的简单使用示例
- 关于UIAlertController 的一些用法
- UIAlertController中TextField的用法
- ios9 xcode7 UIAlertController的常用用法
- UIAlertController小示例
- UIAlertController用法大全
- iOS - UIAlertController相关用法
- UIAlertController详解及用法
- UIAlertController 用法简介
- c#中使用json
- Android 6.0敏感权限新特性及使用方法
- 内部推荐岗位信息201508
- KMP算法
- POJ 3744 Scout YYF I(概率DP+矩阵快速幂)
- UIAlertController的用法示例
- Qt下 QString转char*
- 千里码-刷刷刷
- Tomcat服务器
- win7上安装多个Mysql1
- js静态方法与实例化方法
- 某公司面试题原地堆排序
- jquery ajax 二进制请求
- 设计模式之单例模式