UITextField和UITextView无法输入它们

时间:2012-03-08 07:28:07

标签: iphone ios5 uitextfield uitextview

我动态添加UITextFields和UITextViews,我创建它们运行时,因为我有一个指定TextFields和TextViews的XML文件,然后我从那里构建UI。但在将它们添加到View并运行应用程序后,我无法输入它们。

这是我的添加代码

do {
    if([[TBXML textForElement:[TBXML childElementNamed:@"FlowDirection" parentElement:element]] isEqualToString:@"Output"])
    {
        if([[TBXML textForElement:[TBXML childElementNamed:@"TypeName" parentElement:element]] isEqualToString:@"System.String"] && [[TBXML textForElement:[TBXML childElementNamed:@"ExtendedType" parentElement:element]] isEqualToString:@"None"] && ([TBXML childElementNamed:@"EnumValues" parentElement:element]->firstChild == nil ))
        {
            UILabel *fieldLabel = [[UILabel alloc] initWithFrame:CGRectMake(FirstX, FirstY, 100, 40)];
            fieldLabel.text = [TBXML textForElement:[TBXML childElementNamed:@"Name" parentElement:element]];
            [myView addSubview:fieldLabel];
            [fieldLabel release];
            UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(FirstX + 110, FirstY, 150, 40)];
            [textField setBorderStyle:UITextBorderStyleRoundedRect];
            [textField setEnabled:YES];
            textField.delegate = self;
            [myView addSubview:textField];
            FirstY += 50;
            [textField release];
        }
        else if([[TBXML textForElement:[TBXML childElementNamed:@"TypeName" parentElement:element]] isEqualToString:@"System.String"] && [[TBXML textForElement:[TBXML childElementNamed:@"ExtendedType" parentElement:element]] isEqualToString:@"MultilineText"] && ([TBXML childElementNamed:@"EnumValues" parentElement:element]->firstChild == nil ))
        {
            UILabel *fieldLabel = [[UILabel alloc] initWithFrame:CGRectMake(FirstX, FirstY, 100, 40)];
            fieldLabel.text = [TBXML textForElement:[TBXML childElementNamed:@"Name" parentElement:element]];
            [myView addSubview:fieldLabel];
            [fieldLabel release];
            UITextView *textViewStatus = [[UITextView alloc] initWithFrame:CGRectMake(FirstX +110, FirstY, 150, 100)];
            FirstY += 110;
            [textViewStatus setTextAlignment:UITextAlignmentLeft];

            // For the border and rounded corners
            [[textViewStatus layer] setBorderColor:[[UIColor lightGrayColor] CGColor]];
            [[textViewStatus layer] setShadowColor:[[UIColor grayColor] CGColor]];
            [[textViewStatus layer] setBorderWidth:1];
            [[textViewStatus layer] setCornerRadius:15];
            [textViewStatus setClipsToBounds: YES];
            textViewStatus.delegate = self;
            [textViewStatus setEditable:YES];

            [myView addSubview:textViewStatus];

            [textViewStatus release];
        }
        else
        {
            UILabel *fieldLabel = [[UILabel alloc] initWithFrame:CGRectMake(FirstX, FirstY, 100, 40)];
            fieldLabel.text = [TBXML textForElement:[TBXML childElementNamed:@"Name" parentElement:element]];
            [myView addSubview:fieldLabel];
            UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(FirstX + 110, FirstY, 150, 40)];
            textField.delegate = self;
            [textField setBorderStyle:UITextBorderStyleRoundedRect];
            [textField setEnabled:YES];
            [myView addSubview:textField];
            FirstY += 50;

            [fieldLabel release];
            [textField release];
        }
    }
} while ((element = element->nextSibling));

添加了代表

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    return YES;
}

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
    return YES;
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    return YES;
}

- (BOOL)textViewShouldBeginEditing:(UITextView *)textView
{
    return YES;
}
- (BOOL)textViewShouldEndEditing:(UITextView *)textView
{
    return YES;
}
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    return YES;
}

1 个答案:

答案 0 :(得分:2)

尝试检查'myView'设置正确的框架rect,检查myView是否为UserInteraction启用。