2014-09-25 68 views
0

我已经在我的iOS应用程序中以编程方式创建了两个UITextField s,并分别将它们的文本设置为_minPrice_minPrice变量。UITextFields不允许我编辑文本

_minPrice_maxPrice值在两个字段中正确显示,但在它们上点击不允许用户编辑它们,它们只是保留那些静态值,退格不起作用。有什么关于我的代码,这阻止了文本字段被编辑?

// 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]; 
+0

也许他们看到了如何打字。 ;) – 2014-09-25 21:01:25

回答

1

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

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

enter image description here

如果你确保标签实际上在你添加到的视图内(即不是负面的),编辑就没问题。我所做的只是改变tf负x和y的值,以积极的,他们是编辑:

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

enter image description here

1

试试这个!也许有在文本框

your_textfield.userInteractionEnabled = YES; 

顶部另一种观点认为如果这不起作用

另起一行

[self.view bringSubviewToFront: your_textfield]; 
+0

我已经尝试了你的两条建议,而且我似乎仍然无法编辑这些字段。我认为事实上有一个观点,但我怎么能说出来呢? – Ghobs 2014-09-25 21:21:34

+0

@Ghobs - 我曾经写过一个小程序来查找按钮顶部的视图。这很容易 - 只需要递归搜索来查找重叠视图,然后检查Z顺序。 – 2014-09-26 02:03:28

0

尝试添加在你的文本框的委托方法。像

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

检查它是否转到该行代码。如果没有,也许有一个顶部的观点。或者你可以尝试将textfield带到前面。

[self.view bringSubviewToFront:yourTextfield];

但是,这不是你如何解决这个问题一个很好的例子。只是为了测试它是否有一个视图。