How do I swap views in a single window?

I want to have a single window and use buttons to change between VCs A/B/C in that window. (Let's say it's a game and I want to freely move between a map view, a budget view, and a diplomacy view.) No problem on iOS, but on macOS it always wants to open new windows. I want to know the minimum necessary to either:

A) Create a window and move between VCs with buttons (i.e., back button, menu button, …) or

B) Hook up a split view controller so I can change the VC on the right from buttons on the left.

A skeleton project, a tutorial compatible with XCode 12.5, an annotated screenshot, a backmasked Led Zeppelin album… it's been 3 days and I've tried so many things, read so many tutorials, dev docs, that they're all jumbled in my head making it worse. Please save me before I code again!

Answered by Registration462 in 708080022

FWIW, stuff got in the way and when I came back to it I just used Godot. It was a lot less hassle. But thank you for trying.

Why don't you

  • create the 3 views in IB (A, B, C)
  • Hide all but one (A for instance)
  • to switch to another view (C for instance), hide A and unhide C

I found a demo project at https://developer.apple.com/documentation/appkit/cocoa_bindings/navigating_hierarchical_data_using_outline_and_split_views It's got a lot going on with the hierarchical stuff, but I'm working on figuring out what parts are fundamental.

Hiding views is the easiest to implement.

The views would all be active all the time, just hidden.

What's the problem here ?

But there are many other solutions, it depends what you want precisely.

You could

  • use PageView and swipe between them: https://developer.apple.com/documentation/appkit/nspagecontroller
  • create collectionView, with cell of exact size of the full view : h t t p s : / / w w w.appcoda.com/macos-programming-collection-view/
  • use splitView to have the list of pages in Master on the left and content on the detailView on the right.

Once again, it depends on what user experience you want to provide.

I'm going to move this bit up top and start by thanking you for taking the time to answer me and help out. I really appreciate both it and your calm, respectful attitude.

The problem, as I see it, is that if there's a webpage in a hidden view it may be refreshing over and over wasting power and bandwidth. If there's animation or a video it would also be playing. Sound might be heard (I don't think it would mute automatically). I figured there was an intended way to do it. I, like others, assumed that staying in one window would be the default and you'd have to explicitly open new ones, or at least have an option for which you want.

These suggestions are what I was looking for, along with maybe tabless tabs. I think the splitview or tabless are the likely candidates. I think I'd learn a lot and have a comfortable foothold for my next steps in learning if I could get a grasp of both, even if they're not right.

I think what I need to figure out/find/beg from kindly strangers is:

  1. What, if any, special VC or container I need for the detail view (on the split).

  2. What I need to do to make a button change the view.

  3. How to attach a segue to the view change.

I'm a little confused on 1 because so much is written for iOS which works differently. I'm getting closer on 2. 3 I'm assuming needs to programmatic (i.e., not interface builder), but should be fairly straight forward once I figure out 1 and 2.

I've run into that learning block where the next thing I want to do seems to demand I know ten other things which all demand each other.

I've run into that learning block where the next thing I want to do seems to demand I know ten other things which all demand each other.

That's the usual problem when you start in a new area of iOS. So many thinks intertwined to master.

That's why I usually start with a simple design, just to make it work (my initial proposal). Then progressively evolve it to really do as expected.

Now to your questions:

  1. What, if any, special VC or container I need for the detail view (on the split).

When you create a splitViewController, in Interface builder, it creates automatically 4 VC for you. In older versions of Xcode it also created the classes templates.

Now, look for some tutorial, such as for instance h t t p s : / / w w w.raywenderlich.com/4613809-uisplitviewcontroller-tutorial-getting-started

  1. What I need to do to make a button change the view.

Which button ? The one in the list on the left panel ? If so, that's a basic mechanism, look at tutorial

  1. How to attach a segue to the view change.

I don't understand. You want to have a segue which would be triggered when ? Which view do you mean ?

3 I'm assuming needs to programmatic (i.e., not interface builder)

It depends where do you segue from and to.

Accepted Answer

FWIW, stuff got in the way and when I came back to it I just used Godot. It was a lot less hassle. But thank you for trying.

How do I swap views in a single window?
 
 
Q