在iOS7中,我们已经非常熟悉UIAlertView和UIActionSheet这两种控件。
iOS8的出现,为我们提供了意在可以将二者整合的新控件-----UIAlertController,光看名字会觉得怪怪的,这货难道不是一个controller吗?哈哈,自己之前也没听说过这个新出的UIAlertController,也是在工作的过程中遇到了问题,然后在网上搜索提问发现的。
首先,先说一下遇到的问题吧。
在iOS8下,如果当前界面有键盘弹出--->UIAlertView弹框提示----> 点击alert上的“确定”pop到上一窗体---->你会发现:键盘会闪。同样的操作,在iOS7下正常键盘也没有闪的情况。原因就是在iOS8下,苹果提倡我们用UIAlertController。
我们先通过创建一个稍微复杂一点的alert(如图),来深入了解一下UIAlertController的用法。
创建一个带输入框的alert,代码是从自己写的项目里扒出来的,比较蛋疼,因为写这部分用了两套代码iOS7 和 iOS8。总体来讲,两种方式写出来的效果都挺好的,让用户感觉不到差别。
第一步:创建
typedef NS_ENUM(NSInteger, UIAlertControllerStyle) {
UIAlertControllerStyleActionSheet = 0,
UIAlertControllerStyleAlert
} NS_ENUM_AVAILABLE_IOS(8_0);//UIAlertController的两种Style
UIAlertController 不需要设delegate
- (void)showModifyPasswordView
{
/*在iOS8以后,当当前界面有键盘输入框时,点击alertView切换窗体时,会出现键盘闪一下的问题。iOS8的SDK中,苹果提倡使用UIAlertController取代UIAlertView*/
if([[[UIDevice currentDevice]systemVersion]floatValue] >= 8.0)
{
[self showAlertController];
}
else
{
if(checkPasswordAlertView == nil)
{
checkPasswordAlertView = [[UIAlertView alloc]initWithTitle:@"请输入当前密码" message:@"为保障您的数据安全,修改密码前请填写原密码。" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
checkPasswordAlertView.alertViewStyle = UIAlertViewStyleSecureTextInput;
checkPasswordAlertView.tag = 102;
checkPasswordAlertView.delegate = self;
UITextField *passwordTextField = [checkPasswordAlertView textFieldAtIndex:0];
passwordTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
passwordTextField.keyboardType = UIKeyboardTypeAlphabet;
passwordTextField.returnKeyType = UIReturnKeyDone;
passwordTextField.delegate = self;//shouldReturn 回调
passwordTextField.enablesReturnKeyAutomatically = YES;//内容为空时,回车键不可点击
passwordTextField.autocapitalizationType = UITextAutocapitalizationTypeNone;//不要自动大写任何文本
passwordTextField.autocorrectionType = UITextAutocorrectionTypeNo;//自动纠错关闭
}
[checkPasswordAlertView show];
}
}
- (void)showAlertController
{
if(alertCtrl == nil)
{
alertCtrl = [UIAlertController alertControllerWithTitle:@"请输入当前密码" message:@"为保障您的数据安全,修改密码前请填写原密码。" preferredStyle:UIAlertControllerStyleAlert];
[alertCtrl addTextFieldWithConfigurationHandler:^(UITextField *textField) {
textField.clearButtonMode = UITextFieldViewModeWhileEditing;
textField.keyboardType = UIKeyboardTypeAlphabet;
textField.returnKeyType = UIReturnKeyDone;
textField.enablesReturnKeyAutomatically = YES;//内容为空时,回车键不可点击
textField.autocapitalizationType = UITextAutocapitalizationTypeNone;//不要自动大写任何文本
textField.autocorrectionType = UITextAutocorrectionTypeNo;//自动纠错关闭
textField.secureTextEntry = YES;
}];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
DLog(@"iOS 8 cancel input password ~~~");
UITextField *tmpAlertTextField = (UITextField *)alertCtrl.textFields.firstObject;
tmpAlertTextField.text = @"";
[[NSNotificationCenter defaultCenter] removeObserver:self name:UITextFieldTextDidChangeNotification object:alertCtrl.textFields.firstObject];
okAction.enabled = NO;
}];
okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
[self checkUserOldPassword];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UITextFieldTextDidChangeNotification object:alertCtrl.textFields.firstObject];
okAction.enabled = NO;
}];
okAction.enabled = NO;
[alertCtrl addAction:cancelAction];
[alertCtrl addAction:okAction];
}
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleTextFieldTextDidChangeNotification:) name:UITextFieldTextDidChangeNotification object:alertCtrl.textFields.firstObject];
[self presentViewController:alertCtrl animated:YES completion:nil];
}
//实时探测输入框内容变化,当输入框无内容时,将confirm按钮禁掉
- (void)handleTextFieldTextDidChangeNotification:(NSNotification *)notification
{
UITextField *textField = notification.object;
if(textField.text.length > 0)
{
okAction.enabled = YES;
}
else
{
okAction.enabled = NO;
}
// Enforce a minimum length of >= 5 characters for secure text alerts.
}
第二步:密码输入完毕后,如何取到输入框内容并给变量赋值
- (void)checkUserOldPassword
{
if([MIUtils reachable] > 0)
{
MIUser *tmpUser = [dbhelper getUser];
UITextField *tmppasswordTextField;
if([[[UIDevice currentDevice]systemVersion]floatValue] >= 8.0)
{
tmppasswordTextField = (UITextField *)alertCtrl.textFields.firstObject;
}
else
{
tmppasswordTextField = (UITextField *)[checkPasswordAlertView textFieldAtIndex:0];
}
oldPassword = tmppasswordTextField.text;
centerLoadingView.hidden = NO;
[mssvcClient checkUserOldPasswordWithUserObjectAndPassword:tmpUser andOldPassword:oldPassword];
}
else
{
[self alertViewMessage:@"无网络可用,请检查您的网络"];
}
}
运用UIAlertController可以也可以方便创建actionSheet,方法与创建alert差不多,只需要将上述代码的
addTextFieldWithConfigurationHandler方法移除即可。actionSheet相对简单,不贴代码了。