1
votes

I add UITextFields and UITextViews dynamically, I create them runtime as I have a XML file that specifies TextFields and TextViews and I then build the UI from that. But after adding them to the View and running the app I can't type in them.

Here is my adding code

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));

Added the Delegate

- (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

1 Answers

2
votes

try to check 'myView' set the proper frame rect, check myView is UserInteraction enable.