UITextFields不允许我编辑文本

时间:2014-09-25 20:58:05

标签: ios objective-c uitextfield

我已经在iOS应用中以编程方式创建了两个UITextField,并将其文本分别设置为_minPrice_minPrice个变量。

_minPrice_maxPrice值在两个字段中正确显示,但点击它们并不允许用户编辑它们,它们只是保留那些静态值,退格并不是&#39工作。我的代码是否有任何阻止文本字段被编辑的内容?

    // Min Price
    UITextField *tf = [[UITextField alloc] initWithFrame:CGRectMake(-25, -76, 70, 30)];
    tf.textColor = [UIColor blackColor];
    tf.font = [UIFont fontWithName:@"Helvetica-Neue" size:14];
    tf.backgroundColor=[UIColor whiteColor];
    tf.text= _minPrice;

    tf.textAlignment = NSTextAlignmentCenter;
    tf.layer.cornerRadius=8.0f;
    tf.layer.masksToBounds=YES;
    tf.layer.borderColor=[[UIColor lightGrayColor]CGColor];
    tf.layer.borderWidth= 1.0f;

    // Max Price
    UITextField *tf1 = [[UITextField alloc] initWithFrame:CGRectMake(100, -76, 70, 30)];
    tf1.textColor = [UIColor blackColor];
    tf1.font = [UIFont fontWithName:@"Helvetica-Neue" size:14];
    tf1.backgroundColor=[UIColor whiteColor];
    tf1.text= _maxPrice;

    tf1.textAlignment = NSTextAlignmentCenter;
    tf1.layer.cornerRadius=8.0f;
    tf1.layer.masksToBounds=YES;
    tf1.layer.borderColor=[[UIColor lightGrayColor]CGColor];
    tf1.layer.borderWidth= 1.0f;

    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(100, 200, 400, 400)];
    [view addSubview:tf];
    [view addSubview:tf1];

    [self.view addSubview:view];

3 个答案:

答案 0 :(得分:1)

试试这个!也许textField顶部还有另一个视图

your_textfield.userInteractionEnabled = YES;

如果这不起作用

添加另一行

[self.view bringSubviewToFront: your_textfield];

答案 1 :(得分:1)

您的问题显然是您正在设置的框架......

设置将标签添加到蓝色的视图的颜色会显示您的问题:

enter image description here

如果你确保标签实际上在你添加它们的视图中(即不是负片),编辑就可以了。我所做的就是将tf中的负x和y值更改为正值,并且它们是可编辑的:

UITextField *tf = [[UITextField alloc] initWithFrame:CGRectMake(25, 76, 70, 30)];

enter image description here

答案 2 :(得分:0)

尝试在文本字段上添加委托方法。喜欢

- (void) textFieldDidEndEditing:(UITextField *)textField
{
 //  ADD BREAKPOINT HERE.
}

检查它是否进入该行代码。如果不是,那么可能会有一个视图。或者您可以尝试将文本字段放在前面。

[self.view bringSubviewToFront:yourTextfield];

但这并不是解决问题的一个很好的例子。只是为了测试它上面是否有视图。