UIAlertView 文本字段捕获 onchange [英] UIAlertView textfield capture onchange

查看:26
本文介绍了UIAlertView 文本字段捕获 onchange的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实现自定义 AlertView.

I'm trying to implement custom AlertView.

这个想法是让警报视图带有文本字段和取消按钮.

The idea is to have alertview with textfield and cancel button.

我不能做的是实时检查文本字段中输入的字符.我知道我可以使用 – alertViewShouldEnableFirstOtherButton: 来做到这一点,但我不想要另一个按钮.我希望在没有按钮的情况下做同样的事情.

What i can't do is to check textfield live for entered characters. I know i can do it using – alertViewShouldEnableFirstOtherButton: but i don't want another button. I wish to do the same just without button.

在 android 中,您可以将侦听器添加到文本字段 onchange.

In android you can add listeners to textfields onchange.

尝试使用此 uitextfield 函数来执行此操作,但它没有被实时调用,或者我可能以错误的方式使用它.

Tried to do it using this uitextfield function, but it doesn't get called live or maybe i'm using it in a wrong way.

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    textField = [alert textFieldAtIndex:0];
    if ([textField.text length] == 0)
    {
        NSLog(@"Hello");
        return NO;
    }
    return NO;
}

那么如何正确地做到这一点?

So how to do this properly?

推荐答案

试试这个

    UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"New List Item", @"new_list_dialog")
                                                          message:@"this gets covered" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil];
   UITextField *myTextField = [[UITextField alloc] initWithFrame:CGRectMake(12.0, 45.0, 260.0, 25.0)];
    myTextField.delegate = self;
    [myTextField setBackgroundColor:[UIColor whiteColor]];
    [myAlertView addSubview:myTextField];
    [myAlertView show];
    [myAlertView release];

和文本字段方法

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    NSLog(@" %@", [textField.text stringByReplacingCharactersInRange:range withString:string]);
    return YES;
}

这篇关于UIAlertView 文本字段捕获 onchange的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆