Navigate between sibling detail views of a parent

2019-05-04 18:42发布

问题:

I have a table view that navigates to a detail view when I tap on an item. What I am trying to do is navigate from a detail view to a sibling detail view (for another item in the table) without returning to the parent table view.

For example, if the table view screen has a number of dishes that belong to these types:

  • Sandwiches
  • Soups
  • Salads

When I pick a ham sandwich I go to the detail view for this dish. Here I see that people who like ham sandwiches also like chicken noodle soup and Caesar salads.

I want to go from the ham sandwich to either the chicken noodle soup or Caesar salad detail views without animating back to the table view. How can I do this?

回答1:

When you want to navigate to the next/prev detail view (let's call them sibling detail views), all you need to do it just pop & push the new controller with animated flag set to FALSE


- (void)gotoAnotherDetailView
{
    // Just remove the current 
    [self.navigationController popViewControllerAnimated:FALSE];

    // Create a new 
    DetailViewController* newcontroller = [DetailViewController alloc] initWithDishType:anotherDish];
    [self.navigationController pushViewController:newcontroller animated:FALSE];
}

In this example, DetailViewController is just a dummy controller and you should replace this with however you instantiate controllers in your code.

As I side note, Apple recommends using the delegate pattern for controlling the pushing/poping of controllers. This mean that a controller will not pop itself out of the navigation stack, but will send a message to the parent tableview controller, and that controller will pop&push the new detail view (in one call).