Я добавляю UITextFields и UITextViews динамически, я создаю их во время выполнения, так как у меня есть XML-файл, который определяет TextFields и TextViews, и затем я строю пользовательский интерфейс из этого.Но после добавления их в 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;
}