What is the recommended practice for creating tab view with multiple tabs in Cocoa desktop application

StackOverflow https://stackoverflow.com//questions/9666005

  •  12-12-2019
  •  | 
  •  

Question

What is the recommended practice for creating tab view with multiple tabs. Should I use single XIB file for all the views or have different XIB per views? my preference is to use multiple controllers for each view in the tab view and hence different xib files. How can I achieve it? Is it possible to have multiple views in a single xib file for each tab view, each having different controllers?

Was it helpful?

Solution

Are the tabs always the same, or are they dynamic? If they're always the same, I tend to use a single xib file, just because it's easy. You can still use multiple controller objects — just make them subclasses of NSObject, and drop them all into the same xib.

I did do a quick Google to see if there was an easy way to use NSViewControllers with tab views, and I found this thread. That contains some sample code for how to use them, which would give you a separate controller class and xib per tab. Obviously, I haven't tried that code, so I can't vouch for it, but it might be a good enough start. (In that sample code, it instantiates a new view controller each time you switch tabs, which probably isn't what you want — if it were me, I'd instantiate them the first time a tab was swapped to, and then keep them in a dictionary in the window controller, and on subsequent switches then just pull out the respective view controller that already exists.)

OTHER TIPS

The optimal approach depends on the complexity of your tabs. However, a XIB will soon get messy and cluttered when you add multiple views to them. Also you run the risk of a bloated controller.

I would say the best approach is to have a view and a controller per tab. This will make future extensions to your app much easier to implement.

You'll have a main controller (window or view) that handles the view (or window) that holds your tab view. This controller acts as the delegate for your NSTabView (see NSTabViewDelegate).

Once your controller receives the tabView:didSelectTabViewItem: message, you can create the sub controller that manages that specific tab. This controller will instantiate its associated view (initWithNib) and add it to the view hierarchy. You can repeat this process for each tab in your tab view. You can set things up in such a way that the main (top level) controller maintains a list of each sub controller so you need to create them only once.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top