UINavigationController going directly to 'next

2019-08-18 02:02发布

问题:

I have a UINavigationController with 3 levels of hierarchy: Menu->Submenu->Details. Menu and Submenu are UITableViews whereas Details is some custom view controller.

I want to add 'previous' and 'next' buttons in the Details view that will allow me to directly go to the previous and next items from the current Submenu.

For example let's say I have:

|Menu    |M1 Submenu |SM2 Details
--------------------------------
M1  ->   |SM1        |D1
M2       |SM2   ->   |D2
M3       |SM3        |D3  // Let's say we are here now
                     |D4
// ... etc ...

// The arrows represent items the user selected

So let's say the app is currently showing the D3 Details view controller. This means that we are currently in M1->SM2->D3. Now in D3 if the user presses a 'next' button it should show D4. Similarly if s/he presses the 'previous' button it should show D2.

So the 'next' and 'previous' buttons essentially do the same thing as pressing the navigation controller back button and then selecting the next item in the list.

What would be the best way to implement this? Do I have to pop back up to the submenu and then push another Details controller? Do I call the submenu controller directly from the details view controller (if so how)?

Thanks.

回答1:

There's really no need to call the parent controller. Here's what you can do:

Assuming you keep an array of "detail" items in Submenu Controller, instead of initializing the detail controller with the selected item, initialize it with the array and the selected index.

Then, when the prev/next buttons are pressed, change the selected index and update the view accordingly.