views:

19

answers:

2

I should know this by now, but I am still a bit confused. When my app navigates from one view controller to the next (via the navigation controller) I want to "finalize" the data for the current VC before going to the next VC. The only way I see to intercept the "page swap" is in the [old view viewWillDisappear] -> [newView viewWillAppear] transition. It seems weird, though I guess it works okay.

Is that really the right way to handle navigation transitions? My app is a bunch of VCs which collectively build a database file. Each VC deals with a different aspect of the data.

A: 

I don't know your exact setup, so this may not be useful to you, but I have good experiences with saving data in -(void)textFieldDidEndEditing:(UITextField*)tf, using tf.tag to index the fields. From there I commit the data to a storage class, and have no worries about what happens in the UI.

mvds
A: 

What exactly is involved in the "finalizing" part? I assume you are storing some state in the view controller for various fields and then you want to write that to the database file before going on to the next view?

When it comes to "edit view controllers" I find a nice way to do it is to have the view controller directly write to a simple model object which is injected through a property before pushing it to the nav controller.

So something like:

/* Somewhere in the app delegate, like application:didFinishLaunching */
DatabaseFileModel *model = ...;

viewController1.model = model;
viewController2.model = model;

/* ... */

[self.window makeKeyAndVisible];

Then each view controller writes to this model by setting properties etc. when a text field ends editing or whatever. Having the view controller write straight to the object means you don't need to handle viewWillDisappear etc.

If you do still need to do this however you can add a delegate to the navigation controller and handle these two methods:

– navigationController:willShowViewController:animated:
– navigationController:didShowViewController:animated:

See the UINavigationControllerDelegate documentation for more information.

This will let you keep the logic in one place rather than spread out in each view controller.

Mike Weller