Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does one display a new view controller in the same Mac window?

Tags:

macos

swift

cocoa

I'm fairly new to Mac development and am slightly confused by the new "storyboard" feature in Xcode 6. What I'm trying to do is segue from one view controller to another in the same window. As of right now, all the different NSViewControllerSegues present the view controller in a new window, be it a modal or just another window. What I'd like to do is just segue within the same window, much in the same way one would on iOS (though an animated transition is not crucial). How would this be achieved?

like image 411
aaplmath Avatar asked Jan 25 '15 17:01

aaplmath


People also ask

What is instantiate view controller?

instantiateViewController(withIdentifier:)Creates the view controller with the specified identifier and initializes it with the data from the storyboard.


1 Answers

If you provide a custom segue (subclass of NSStoryboardSegue) you can get the result you are after. There are a few gotchas with this approach though:

  • the custom segue will use presentViewController:animator so you will need to provide an animator object
  • because the presented view is not backed by a separate Window object, you may need to provide it with a custom NSView just to catch out mouse events that you don't want to propagate to the underlying NSViewController's view
  • there's also a Swift-only glitch regarding the custom segue's identifier property you need to watch out for.

As there doesn't seem to be much documentation about this I have made a small demo project with custom segue examples in Swift and Objective-C.

enter image description here

I also have provided some more detail in answer to this question.

like image 128
foundry Avatar answered Oct 04 '22 05:10

foundry