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

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

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

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

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

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

    //自己定义一个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

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

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

alertViewStyle 属性有以下三种选项:

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

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

UIAlertViewStylePlainTextInput

(UIAlertViewStylePlainTextInput)

UIAlertViewStyleSecureTextInput

(UIAlertViewStyleSecureTextInput)

UIAlertViewStyleLoginAndPasswordInput

(UIAlertViewStyleLoginAndPasswordInput)

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

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

-(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];
    
}

 

转载于:https://my.oschina.net/5951008876/blog/690344

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值