3

I have a Mac app that needs to be based on multiple modules. That is, a single window with multiple views, and the default view with a menu. That menu should open one module on the default window and then if I select another module, the contents of the window should change with another view. Those views also have different states, so I made multiple views for each module.

In a nutshell, my app is a single AppDelegate.h/.m, a single xib file, with one NSWindow object and multiple NSView views. Those views have different states, so I load different other related NSViews.

To load a view, I use [window setContentView:viewNameView]; which I know that causes the old NSView to lose state, so I need to keep them all in memory for each module.

Is this the right approach?

Thank you!

MariusP
  • 472
  • 4
  • 17

3 Answers3

2

You don't describe how and where you want the menu but a widely used method is to have a sourcelist on the left and the content on the right. You see this everywhere including Apples own apps.

If you create a sourcelist on the left of your window and place an NSBox on the right side.

enter image description here

Set up the sourcelist (NSOutlineView) to react to - outlineViewSelectionDidChange: which is an NSOutlineView delegate method.

Here you can check the identifier on the selected item in the menu and set the content view for the NSBox accordingly with - setContentView:

Here's a great introduction to using NSOutlineViews for anyone interested.

Edit: Depending on how many views you have it might be easier to have an NSTabView (in tabless mode) and just switch tabs in the - outlineViewSelectionDidChange: method. This is also widely used and the user won't see the difference.

d00dle
  • 1,276
  • 1
  • 20
  • 33
2

You will want to look up NSWindowController for managing your window and xib, and NSViewController for managing views. The app delegate shouldn't do much (in fact you probably could remove the header file and merge it with .m).

Some references to look at:

https://www.mikeash.com/pyblog/friday-qa-2013-04-05-windows-and-window-controllers.html https://developer.apple.com/library/mac/samplecode/ViewController/Introduction/Intro.html

Zorg
  • 978
  • 6
  • 10
1

Yes that will work. What you may end up needing as well, is a custom Navigation Controller. Unfortunately Cocoa doesn't have an NSNavigationController, so you'll have to write something on your own. But basically yeah what you'll do is swap out the contentView with the next view you want to display-- and keep a stack of views you've navigated to so you can support going back (or you could use a dictionary to add transition keys to create strongly linked transitions)

Here's an good example somebody posted in a previous thread-- if you just search for Cocoa Mac Navigation Controller you should find some helpful results :)

Mac OS X Cocoa multiview application navigation

Another thing that you may want to keep in mind, which came up for me, is if your views are of different sizes. If they are, and you are using auto-layout, you will need to update the constraints to resize the window appropriately as views are swapped out

Community
  • 1
  • 1
A O
  • 5,516
  • 3
  • 33
  • 68