1

iOS 7 introduces edge swiping to navigate backwards in a navigation controller (so long as you don't override the leftBarButtonItem, among other things).

Imagine I'm implementing an app similar to Apple Notes. In Apple's app, once the user taps into a note, there is a Back button on the top left, and a Done button on the top right. This view controller is automatically saved after the user navigates away. Even if the user swipes backwards while still editing the note, it will auto-save.

One way to implement this is to write to disk on each key stroke. However, I'm looking for a more efficient implementation.

Which method should I override to perform my auto-save?

To be clear, the auto-save code should be invoked in these three cases:

  • The user taps the Back button
  • The user swipes backwards
  • The user taps the Done button
Community
  • 1
  • 1
Senseful
  • 86,719
  • 67
  • 308
  • 465

1 Answers1

1

So far, I've been implementing the save logic in viewWillDisappear, this minimizes the amounts of writes, and also takes care to save if the user kills the app.

Additionally, instead of implementing save logic in the Done button, the Done button can simply call:

[self.navigationController popViewControllerAnimated:YES];

... (e.g. via a delegate, or unwind segue) and it will automatically save as well. All three code paths will go through viewWillDisappear.

Although it could technically be implemented in viewDidDisappear, if the previous view controller needs to display the updated data, this method will be too late.


For more complex view controllers (e.g. those that have destructive behavior once the view controller is dismissed), there are several other things to consider:

  1. Should the save be called if a modal is presented above the current view controller? (e.g. In Apple Notes, imagine that the share button launched a modal; should it save when you tap on this button?). If it is important not to save at this time, you can read the value of presentedViewController. If it has a value, that means viewWillDisappear is being called because a modal is being presented above it.
  2. Should the save be called if a view controller is pushed on to the current navigation stack? (e.g. Again, in Apple Notes, if the share button used a push instead of a modal, should it save?) You can check the value of isMovingFromParentViewController in this case.
  3. If the user taps the home button to quit your app, should it save?
  4. If the user begins swiping backwards then cancels the swipe to stay on the current screen, should it save?
  5. Imagine the view controller is in a modal, when the modal is dismissed should the destructive action happen? Check isBeingDismissed in this case.
  6. Same as above, but imagine the view controller is the second page in a navigation controller than is being presented.
Community
  • 1
  • 1
Senseful
  • 86,719
  • 67
  • 308
  • 465