R – Navigation view controller question


Below is what I want to implement:

  1. The main screen of my app is a UITableView. Each row in the table view is a category, when you click the detail disclosure button in the row, you can see a bunch of items under this category in the category detail view.

  2. Now in the main screen, I click the "+" button in navigation bar to create a new category. (The new category will become a new row in the table view). The app then take me to the "Add Category" view. (I used presentModalViewController)

  3. In the "Add Category" view, I set something, then click "Save" button to dismiss the "Add Category" view. (I used dismissModalViewControllerAnimated)

  4. Usually after I click "Save" button, the app will take me back to the main view and I will see a new row in the table.

  5. But that's not what I want to go, what I want is – after I click the "save" button, the "Add category" view will be dismissed but not return to the main view. Instead, I will see the details of the new-created category so I can continue to add items under this category. The result is just like "I return to the main view and then click the detail disclosure button of the new-created row (category)".

  6. Does any one know how to realize that? Thanks!

Best Solution

You can do this by doing a little decoupling of one screen from another:

- Create a custom delegate and protocol for your category creation modal dialog. Something simple like:

@protocol CategoryCreationProtocol
- (void) categoryAddDone:(NSString *)category;
- (void) categoryAddCancelled;

@interface CategoryCreationDialog : UIViewController {
    id<NSObject, CategoryCreationProtocol> categoryDelegate;

@property (assign) id< CategoryCreationProtocol, NSObject> categoryDelegate;

- In the modal dialog when the user taps the 'Save' button, after dismissing the view controller, you invoke the delegate method:

if (categoryDelegate && [categoryDelegate 
            [categoryDelegate categoryAddDone:newCategory];

Also, something similar for the Cancel button.

- Your main controller implements the categoryAddDone method and sets itself to be the categoryDelegate for the modal dialog.

- At runtime, when the user taps Save the delegate method is invoked so your main view is notified that something has happened and it can push the right view into place and even jump to the proper category.

- In your case, as soon as the category creation is done, the main view controller is notified, so it can release the category creation dialog and push the category detail view into the stack. The user sees the modal dialog disappear and slides right into the detail view.

- In general, using delegate/protocols for push navcontroller and modal dialogs is a really handy pattern for making decoupled and reusable views. This way they can be invoked from a variety of places. To make it consistent, you may also want to have a show method on each modal dialogs and pushed view controllers that the caller can invoke. This way there's a consistent way to get into and a consistent way to get notified that the user is done with it.

Related Question