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

[新旧版本]iOS开发小记:带输入框(TextField)的UIAlertView

2016-06-13 00:00 429 查看
摘要: iOS开发小记:带输入框(TextField)的UIAlertView

iOS开发小记:带输入框(TextField)的UIAlertView

光写这篇文章的标题我就至少三次把Ctrl键按错成Alt(或Win)键,因为刚从Mac下来,恼火。。。

昨天写项目的时候有个地方需要用到UIAlertView(警告)上有一个输入框(UITextField),第一反应就是去Code4App上翻,还好翻到两个用得上的Demo,但是下下来很多错误,包括没有ARC什么的,一大堆警告。

于是就想着自己简单定制一个AlertView上添加UITextField,由于项目很简单,就没做很复杂,直接在alertView上addSubview个输入框,然后点击按钮的时候再获取出来这个输入框的值就ok了。是不是很简单呢?

下面是最初的想法,实现的代码:

[code=plain]    //自己定义一个UITextField添加上去,后来发现ios5自带了此功能
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"类别修改" message:@" " delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:@"修改",nil];
UITextField * txt = [[UITextField alloc] init];
txt.backgroundColor = [UIColor whiteColor];
txt.frame = CGRectMake(alert.center.x+65,alert.center.y+48, 150,23);
[alert addSubview:txt];
[alert show];

这张图就是上述代码的效果,可以看到这个输入框不是很美观,棱角很明显,没圆角,没阴影,控制也不是很方便。



这样其实是一个很笨的方法,没有封装起来一个独立的AlertView,只想投简单搞定一个类似这样功能的东西,不过还是有用的,记录下来以后方便改造。

后来发现经高人指点,ios5已经自带了上述功能,甚至更丰富,那就是 Alert 的 alertViewStyle 属性。

alertViewStyle 属性有以下三种选项:

UIAlertViewStylePlainTextInput - 添加一个普通输入框
UIAlertViewStyleSecureTextInput - 密码输入框
UIAlertViewStyleLoginAndPasswordInput - 普通输入框加密码输入框

下面分别来看看这三种属性的效果:



(UIAlertViewStylePlainTextInput)



(UIAlertViewStyleSecureTextInput)



(UIAlertViewStyleLoginAndPasswordInput)

可以看到自带的文本+密码输入框弹出的键盘有点点不一样,稍带透明。

初始化AlertView后,通过设置这个属性,达到AlertView上出现输入框的效果,然后再添加UIAlertViewDelegate代理,在下面棉纺就可以获取到这个文本框。

[code=plain]-(void)alertView : (UIAlertView*)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
//得到输入框
UITextField *tf=[alertView textFieldAtIndex:0];
}


//http://www.cocoachina.com/ios/20141126/10320.html

[新版本]带有文本框的alert、直接复制过去

-(void)createAlertViewTextField{

UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"titler" message:@"wsssa" preferredStyle:UIAlertControllerStyleAlert];

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

NSLog(@"%@",textField.text);
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(alertTextFieldDidChange:) name:UITextFieldTextDidChangeNotification object:textField];
textField.placeholder = @"请输入密码";
textField.secureTextEntry = YES;
textField.borderStyle = UITextBorderStyleBezel;

}];

UIAlertAction *activit = [UIAlertAction actionWithTitle:@"ok" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

NSLog(@"%@", alertController.textFields.firstObject.text);
}];
activit.enabled = NO;
[alertController addAction:activit];

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

//
//
// UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"''" message:nil delegate:self cancelButtonTitle:@"ok" otherButtonTitles:@"yes", nil];
//
//
// alertView.alertViewStyle = UIAlertViewStyleLoginAndPasswordInput;
//// UITextField * txt = [[UITextField alloc] init];
//// alertView.backgroundColor = [UIColor blackColor];
//// alertView.frame = CGRectMake(alertView.center.x+65,alertView.center.y+48, 150,23);
//// [alertView addSubview:txt];
// [alertView show];
//

}
- (void)alertTextFieldDidChange:(NSNotification *)notification{
UIAlertController *alertController = (UIAlertController *)self.presentedViewController;
if (alertController) {
UITextField *login = alertController.textFields.firstObject;
UIAlertAction *okAction = alertController.actions.lastObject;
okAction.enabled = login.text.length > 2;
}
}
-(void)viewDidAppear:(BOOL)animated{

[self createAlertViewTextField];

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