I have a modalviewcontroller and have two buttons on it: Cancel and Save. There is a UITextField which is editable.
Whenever I click on save button I do save it but the text doesnot get save because when I click on the button to open the modalviewcontroller, the text disappears. Dont know whats wrong with my code.
Here is my code :
- (void)viewWillAppear:(BOOL)animated {
self.cancel = self.navigationItem.leftBarButtonItem;
self.save = self.navigationItem.rightBarButtonItem;
UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithTitle:@"Cancel" style:UIBarButtonItemStylePlain target:self action:@selector(cancelAction)];
self.navigationItem.leftBarButtonItem = cancelButton;
[cancelButton release];
UIBarButtonItem *saveButton = [[UIBarButtonItem alloc] initWithTitle:@"Save" style:UIBarButtonItemStylePlain target:self action:@selector(saveAction)];
self.navigationItem.rightBarButtonItem = saveButton;
[saveButton release];
[super viewWillAppear:animated];
}
-(IBAction) cancelAction{
[[self parentViewController] dismissModalViewControllerAnimated:YES];
}
-(IBAction) saveAction{
NSString *text = [textFieldBeingEdited text];
[textFieldBeingEdited setText:text];
[self setDescription:text];
[[self parentViewController] dismissModalViewControllerAnimated:YES];
}
I am not sure if I have to use the following code to save the text in textfield :
- (void)textFieldDidEndEditing:(UITextField *)textField
{
[self dismissModalViewControllerAnimated:YES];
}
I don’t see in your code anywhere that you are setting the text in the
UITextFieldwhen the view appears. You can add some code to yourviewWillAppearmethod to set the text when the view appears, such as:(It looks like you are storing the
NSStringin the description variable.)Also, if this is some kind of string that could be considered a User Default, you can store the string there. I’ve used this before and it populates my
UITextFieldnicely.Code Example: