iOS开发之UIAlerView与UIAlertController的使用

一、UIAlerView

UIAlertView已经过期了,9.0或以上的版本使用会提示过期提醒,但是可以正常使用。最好还是改为使用UIAlertController替代。


warning

1. 定制弹框的标题、内容和按钮

...
@property (nonatomic, strong) UIAlertView *alert;
...

self.alert = [[UIAlertView alloc]initWithTitle:@"UIAlertView" message:@"此UIAlertView已经过期,应该使用UIAlertController替代" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"好的",@"知道了",nil];

2. 定制弹框的样式

有四种样式:
UIAlertViewStyleDefault : 没有输出框,只是普通的弹框
UIAlertViewStyleSecureTextInput 有一个安全密码输入框
UIAlertViewStylePlainTextInput 有一个普通文本输入框
UIAlertViewStyleLoginAndPasswordInput 有两个输入框:文本和密码

[self.alert setAlertViewStyle:UIAlertViewStyleLoginAndPasswordInput];
[self.alert show]; // 执行显示弹框

showAlert

3. 弹框中的普通文本和密码输入框使用

// 当前控制器遵守代理:<UIAlertViewDelegate>
...
@property (nonatomic, strong) UITextField *textField; @property (nonatomic, strong) UITextField *passwordField; ... self.textField = [self.alert textFieldAtIndex:0]; self.passwordField = [self.alert textFieldAtIndex:1]; self.textField.placeholder = @"用户民"; self.passwordField.placeholder = @"密码"; #pragma mark - UIAlerViewDelegate /** * 根据用户按下不同按钮执行不同的逻辑 * * @param alertView 弹框 * @param buttonIndex 按钮索引 */ - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { if (buttonIndex == 1) { NSLog(@"按下了 好的 按钮"); NSLog(@"用户名:%@,密码:%@",self.textField.text,self.passwordField.text); }else if (buttonIndex == 2) { NSLog(@"按下了 知道了 按钮"); }else if (buttonIndex == 3) { NSLog(@"按下了 赞一个 按钮"); } } /** * 决定第一个按钮是否启用 * * @param alertView 弹框 * * @return 布尔值 */ - (BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView *)alertView { if ([[self.textField text]length] == 0 || [[self.passwordField text]length] == 0) { return NO; } return YES; }

4. 其他属性

应用退到后台弹框还是没有消失,这个方法可以给你控制按钮的选择

[self.alert dismissWithClickedButtonIndex:2 animated:YES];
[self.alert addButtonWithTitle:@"赞一个"];
NSLog(@"按钮的个数为:%ld",[self.alert numberOfButtons]); NSLog(@"取消按钮的索引为:%ld",[self.alert cancelButtonIndex]); NSLog(@"弹框是否可见:%d",[self.alert isVisible]); NSLog(@"第2个索引的按钮标题为:%@",[self.alert buttonTitleAtIndex:2]); NSLog(@"第一个其他按钮的索引:%ld",[self.alert firstOtherButtonIndex]);

二、UIAlertController

在iOS 8.0 版本中新增了 UIAlertController 来取代 UIAlertView,它集合了alertactionSheet

1. 定制弹框的标题、内容和按钮

- (void)showAlert {
  __weak typeof(self) weakself = self; self.alert = [UIAlertController alertControllerWithTitle:@"UIAlertController" message:@"8.0以上使用UIAlertController替换UIAlertView" preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *cancleAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) { NSLog(@"点击了取消按钮"); [[NSNotificationCenter defaultCenter]removeObserver:weakself name:UITextFieldTextDidChangeNotification object:nil]; }]; UIAlertAction *defaultAction = [UIAlertAction actionWithTitle:@"好的" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { NSLog(@"点击了好的按钮"); NSLog(@"用户名:%@",[[[self.alert textFields]firstObject]text]); NSLog(@"密码:%@",[[[self.alert textFields]lastObject]text]); [[NSNotificationCenter defaultCenter]removeObserver:weakself name:UITextFieldTextDidChangeNotification object:nil]; }]; // 先冻结 “好的” 按钮,需要用户输入用户名和密码后再启用 [defaultAction setEnabled:NO]; [self.alert addAction:cancleAction]; [self.alert addAction:defaultAction]; // 添加文本输入框 [self.alert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) { textField.placeholder = @"请输入用户名"; [[NSNotificationCenter defaultCenter]addObserver:weakself selector:@selector(handleTextFieldDidChanged:) name:UITextFieldTextDidChangeNotification object:nil]; }]; // 添加密码输入框 [self.alert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) { textField.placeholder = @"请输入密码"; textField setSecureTextEntry:YES]; }]; [self presentViewController:self.alert animated:YES completion:nil]; } - (void)handleTextFieldDidChanged:(NSNotification *)notification { UIAlertController *alertController = (UIAlertController *)self.presentedViewController; if (alertController) { UITextField *textField = alertController.textFields.firstObject; UIAlertAction *action = alertController.actions.lastObject; action.enabled = textField.text.length > 0; } }

转载于:https://www.cnblogs.com/lys-iOS-study/p/5690579.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值