Storing UITextField contents before view pops

后端 未结 3 469
执笔经年
执笔经年 2021-01-04 06:36

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

3条回答
  •  有刺的猬
    2021-01-04 07:02

    OK, I figured out a simple solution after a lot of web-surfing, forum reading, and manual reading. It was, as I suspected, very simple, only one line of code added. In the viewWillDisappear method of the EditViewContorller I simply added:

        [self.view.window endEditing: YES];
    

    Now textFieldShouldEndEditing, textFieldEditingEnded, and textFieldDidEndEditing all get fired off before the viewWillAppear of the master view does.

    So now the viewWillDisappear method looks like:

    - (void) viewWillDisappear: (BOOL) animated {
        [super viewWillDisappear: animated];
        NSLog( @"In viewWillDisappear" );
        // Force any text fields that might be being edited to end so the text is stored
        [self.view.window endEditing: YES];
    }
    

    And the methods already in place to handle the 'Return' on the keyboard also handle the 'Back' button on the Navigation controller.

    Thank you Aaron and Jeff for your assistance and helping me think this through.

提交回复
热议问题