Note that there are some explanatory texts on larger screens.

plurals
  1. POStoring UITextField contents before view pops
    text
    copied!<p>I am sure this is in the Apple documentation or must have been answered somewhere on this forum, since it seems so basic, but I could not find it nor a particularly elegant solution myself. </p> <p>What I have is a UIViewController that pushes an editing view on its navigation stack. The editing view has a bunch of UITextFields in it. If one of them is being editing when the back button is touched, the original view's ViewWillAppear method is called before either the UITextField delegate methods of <code>textFieldShouldEndEditing</code> or <code>textFieldDidEndEditing</code>, or the IB linked action <code>textFieldEditingEnded</code> method are called.</p> <p>Here is some code that I hope will make it clearer:</p> <p>In the UIViewController:</p> <pre><code>- (void) viewWillAppear: (BOOL) animated { [super viewWillAppear: animated]; NSLog( @"Entering view will appear for master view" ); nameLabelField.text = objectToEdit.name; } - (IBAction) editMyObject: (id) sender { NSLog( @"Editing the object" ); EditViewController *evc = [[EditViewController alloc] initWithNibName: @"EditTableView" bundle: nil]; evc.editedObject = objectToEdit; [self.navigationController pushViewController: evc animated: YES]; [evc release]; } </code></pre> <p>In the EditViewController &lt;UITextFieldDelegate>:</p> <pre><code>- (void) viewWillAppear: (BOOL) animated { [super viewWillAppear: animated]; nameField.text = editedObject.name; } - (void) viewWillDisappear: (BOOL) animated { [super viewWillDisappear: animated]; NSLog( @"In viewWillDisappear" ); if( [self.navigationController.viewControllers indexOfObject: self] == NSNotFound ) { NSLog( @"-- We are not in controller stack... the back button has been pushed" ); } } - (BOOL) textFieldShouldEndEditing: (UITextField *) textField { NSLog( @"In textFieldShouldEndEditing" ); // Store text field value here??? // editedObject.name = nameField.text; return YES; } - (void) textFieldDidEndEditing: (UITextField *) textField { NSLog( @"In textFieldDidEndEditing" ); // Store text field value here??? // editedObject.name = nameField.text; } - (IBAction) textFieldEditingEnded: (id) sender { NSLog( @"In textFieldEditingEnded" ); // Store text field value here??? // editedObject.name = nameField.text; } </code></pre> <p>The log ends up with:</p> <blockquote> <p>[...] Entering view will appear for master view<br> [...] Editing the object<br> [...] In viewWillDisappear<br> [...] -- We are not in controller stack... the back button has been pushed<br> [...] Entering view will appear for master view<br> [...] In textFieldShouldEndEditing<br> [...] In textFieldEditingEnded<br> [...] In textFieldDidEndEditing </p> </blockquote> <p>I want to set <code>self.editedObject.name = nameField.text</code> before the label gets set in <code>viewWillAppear</code> for the UIViewController. </p> <p>I thought about in the viewWillDisappear method for the EditViewController checking to see if any of my text fields are currently the first responder and if so getting their text and storing it, but this seems like such a kludge that will be a pain to maintain if I add or change text fields. </p> <p>I can also implement the <code>textFieldEditingChanged</code> IB linked action to set the text in the edited object after every keystroke but this is also quite a bit of overhead since I have to figure out which text field I am in every keystroke (remember I only showed <code>name</code> but there are a whole bunch of them).</p> <p>All I need is for the editing to be ended or to know the editing will be ended before viewWillAppear is called in the UIViewController so the nameFieldLabel is properly set.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload