I have the following code for UITextField. The problem I’m having is that the text does not persist. For example, when I present a modal view, then dismiss it, the UITextField does not have the text anymore in it. I want the text to remain there until I dimiss that view with the text field.
I am displaying the UITextField like this:
UITextField *nameTextField = [[UITextField alloc] initWithFrame:CGRectMake(110, 10, 185, 30)];
nameTextField.delegate = self;
[nameTextField addTarget:self action:@selector(editingEnded) forControlEvents:UIControlEventEditingDidEnd];
[nameTextField setEnabled: YES];
self.myTextField = nameTextField;
[nameTextField release];
Then I have:
- (void)editingEnded
{
NSString *tempRoutineName = self.myTextField.text;
self.routineName = tempRoutineName;
[tempRoutineName release];
}
Instead of editingEnded, Implement the
UITextFieldDelegateprotocol. Go to thetextFieldDidEndEditingmethod and reassign the value of text in it.Like,
Now in the in the
viewDidLoadmethod or theviewWillAppearmethod, go ahead and assign this value back to the textField.If necessary use
[tableView reloadData]if this is used in a tableView or use[self reloadInputViews](if necessary).Then again, its all logical. Nothing too complex in code.