How to disable Views in Cocoa app?

So, I'm starting to learn how to use Cocoa. I think I did it, but I'm stuck on creating and switching views. I am rewriting a game that I made a little back for practice. All I want is a single window (preferably not resizable), and I want to be able to turn off views for different screens in the game.

Firstly, I have a main menu ("Start the game", "High scores", "Exit"). Then I need a window for each screen (gameplay screen, Highscore screen).

I am embarrassed about how to do this. I searched for NSViewController, thinking that it controls the views, but it is not. It controls only one view, actually loading it. I do not understand why I will need to use NSViewController. Could I just have a window class that contains several subclasses of NSView and load them like that? I'm not sure I understand the purpose of ViewController.

Do I really need the Window class to subclass NSWindowController? I tried to follow the example of the Apple ViewController example, and it has a window controller class, which is a subclass of NSWindowController. I do not understand what the purpose of subclassification is. Everything seems to add NSWindowController - initWithPath:(NSString *)newPath , but I don't see its use when I can simply edit the plist file to open the window at startup. Example Apple also has an NSView variable and an NSViewController variable. You do not need only one variable to store the current view?

Thanks in advance guys, I'm really confused about how this works.

+6
objective-c cocoa nsviewcontroller macos
source share
4 answers

You use NSWindowController and NSViewController to control the window or view, because you will need to subclass NSWindow or NSView for the new "views" of the window or view. For example, if you want to use a circular window, you must subclass NSWindow . If you just want to have controls in the window, you are a subclass of NSWindowController .

Same with NSViewController : Normally, NSViewController's will have some base class, such as NSView (or perhaps your own subclass of NSView , which draws its own background). NSViewController manages the interaction between the subzones of this view, allowing it to act as a unit in your larger application.

Think of it this way: Create images and they turn raw input events into higher-level actions. Controllers provide information for viewing and process actions.

Composition in this way can greatly improve the modularity of your code, simplifying design, writing, and debugging.

+5
source share

Looks like you're trying to swap the contents of a window? If so, you can use -[NSView replaceSubview:with:] with -[NSWindow contentView] as the receiver.

Say that you have a title page with the name titleView and a menu page with the name menuView , and you want to change them in the main application window and exit it. If the title page is visible and the user clicks the "main menu" button or link, you should add something like this in the button delegation method:

 [[[NSApp mainWindow] contentView] replaceSubview:titleView with:menuView]; 

Two things to know about:

  • In this case, the old titleView freed by this call. If you want it to continue to be available, you will need to retain until it is replaced.
  • The parent view will not resize if your pages have different sizes. Resizing the frame in the window and therefore the contentView is simple enough, but you can add some Core animation to the mix to give it some style.

Hope this helps!

+4
source share

Use the following functions defined in UIVew (which is part of your existing window)

 - (void)addSubview:(UIView *)view - (void)removeFromSuperview 
+1
source share

I am new to cocoa, but I think your application really does not require NSViewController. NSViewController plays the role of a “controller” in the MVC design pattern. Thus, he "controls" all actions - the logic of adhesives - within the same presentation.

For example, in a view with several controls: buttons, tables, check boxes :, field field selections, etc., detailed interactions between these controls that require binding and updating are likely to be detected. For example, clicking a button loads a database selection with error handling and validation. This glue logic is part of the NSViewController class.

In your application, it looks like each view is a simple page with little need to control the viewing level. So you probably need one NSWindowController, a controller to handle logic and events to go from page to page.

One way to handle multiple views that fill a single window is to use NSTabView and set the tab style to “no borders” in IB. Then use the button action to select the desired NSTabViewItem. At design time, set the tab style to “top tabs”, and IB allows you to enter the tabs that you want to display.

0
source share

All Articles