简体   繁体   中英

iPad Navigation between different subviews

I'm an experienced programmer, but fairly new to iOS/ObjC. I need to add a feature to an existing iPad app, and I'm not sure how to go about it.

The app has a view (let's call it InfoView) with several subcomponents, one of which is a tableview (TableView), and two of which are buttons (ButtonA and ButtonB) (plus other stuff that's irrelevant to this problem).

When ButtonB is pressed, TableView needs to be replaced by some other view (let's call it DetailViewA) with various items on. (ButtonA takes us back to TableView - these buttons act like pseudo tab buttons) When one of the items in DetailViewA is selected, DetailViewA needs to be replaced by DetailViewB. When DetailViewB is closed, detailViewA should re-appear.

I believe I can do the toggling between TableView and DetailViewA by simply hiding the appropriate one when ButtonA/ButtonB is pressed, but I don't know how to nicely deal with drilling down into DetailViewB and back up again (preferably with some sliding animation) - I thought that perhaps I could maybe do this with a UINavigationController, but after some research I still can't see how I'd embed a UINavigationController inside a subview.

A picture tells a thousand words, so here's my rough sketch of what I'm talking about:

+--------------------------------------------------------------------------+
|           InfoView                 |                TopView              |
|     [ButtonA]      [ButtonB]       |                                     |
|    (show table)  (show detail)     |                                     |
| ---------------------------------- |                                     |
|                                    |                                     |
|   this area contains TableView     |                                     |
|                                    |                                     |
|   when ButtonB is touched this     |           other stuff here          |
|   needs to display DetailViewA     |                                     |
|   with the ability to drill down   |                                     |
|   deeper to DetailViewB  (and      |                                     |
|      maybe deeper)                 |                                     |

Or hierarchically: ( [ ] indicating uncertainty / unimplemented)

TopView
\ InfoView
   \ ButtonA
   | ButtonB
   | TableView           - Shown when ButtonA pressed
   | [ MoreInfoView? ]   - Shown when ButtonB pressed
      \ [ DetailViewA ]
      | [ DetailViewB ]

What's the best way to implement this functionality?

You can embed a navigation controller inside a view using child view controllers . Set up your navigation controller with a controller for DetailViewA as the root view controller, then call this code to embed the navigation controller:

[self addChildViewController:self.detailNavigationController]; 
self.detailNavigationController.view.frame = frameOfTableView;
[self.view addSubview:self.detailNavigationController.view];
[self.detailNavigationController didMoveToParentViewController:self];  

You would hide the original table view when the button was pressed and show the navigation controller. The navigation controller would then handle the "drilling down" between detail views.

Note that you will need view controllers for DetailViewA and DetailViewB, not just views.

Hope this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM