Adding views or windows to MainWindow

后端 未结 1 817
野性不改
野性不改 2021-02-01 08:35

I\'m stumbling over some basic concepts, that I cannot grasp. I hope, somebody can clear some things up for me, as I\'ve found no resource, that would explain that. OR maybe, it

相关标签:
1条回答
  • 2021-02-01 09:14

    Not understood so far: I am trying a single window application. I can do everything in the appDelegate (works fine so far) and I can have an additional controller, to whcih I can connect UIElements from the MainWindow to (works although fine so far). BUT: What I'd really like to do, is have a MainWIndow, that only has the menu, and a separate controller and nib (possibly even more than one of both later on), that are loaded and added subsequently.

    For the sake of clarity, MainWindow is a nib file so I’ll refer to it as MainWindow.nib. It is the standard nib file name specified in the application’s Info.plist file as the nib file to be loaded when the application starts.

    By default, Xcode creates a MainWindow.nib file that contains both the main menu and the main window. You’re free to delete the window from MainWindow.nib file and have another nib file to hold that window. Let’s call this other nib file MyWindow.nib.

    Use NSWindowController or NSViewController? And why? (I'd tend to viewController)

    Since you’ll have a separate nib file to hold a window, you’ll use NSWindowController. Create a subclass of NSWindowController, e.g. MyWindowController, which will be responsible for controlling the window stored in MyWindow.nib. This subclass will have outlets pointing to the user-interface elements in that window, and potentially other objects you define in MyWindow.nib.

    When doing this, it’s important that you do the following in MyWindow.nib:

    • Set the file’s owner to be of type MyWindowController;

    • Connect the window outlet in file’s owner to the window object.

    NSViewController is used to manage a view, normally loaded from a nib file, and it doesn’t apply to windows.

    You can repeat this — window in a .nib file, subclass of NSWindowController to manage that window — for as many windows as needed.

    What, where and how to instantiate (presumably in the didFinishLaunching of the appDelegate?)

    Since you want a single window in your application, one option is for your application delegate to hold a reference (instance variable, declared property) to the single window controller that manages that window. Then, in -applicationDidFinishLaunching:, instantiate said controller.

    For example:

    // MyAppDelegate.h
    
    @class MyWindowController;
    
    @interface MyAppDelegate : NSObject <NSApplicationDelegate>
    @property (retain) MyWindowController *myWindowController;
    @end
    

    and:

    // MyAppDelegate.m
    
    #import "MyAppDelegate.h"
    #import "MyWindowController.h"
    
    @implementation MyAppDelegate
    
    @synthesize myWindowController;
    
    - (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
        self.myWindowController = [[[MyWindowController alloc]
            initWithWindowNibName:@"MyWindow"] autorelease];
        [self.myWindowController showWindow:self];
    }
    
    …
    @end
    

    If you want more windows, it’s just a matter of declaring instance variables/properties to hold their corresponding controllers and instantiate the controllers like above.

    How to add window or view to the one and only main-window instead of having a second, independent window (I'm not yet up for multiDocumentApps)

    Are you sure you want to add a window to the main window? If so, that’d be called an attached window. You can use the mechanism above (place the window in its own nib file, have a subclass of NSWindowController to manage it, have your original MyWindowController hold a reference (instance variable, declared property) to the attached window controller, and instantiate it/load the attached window nib file when appropriate) and then use -[NSWindow - addChildWindow:ordered:] to attach the secondary window to the main window.

    For example, considering MyWindowController has a declared property secondaryWindowController, in MyWindowController.m:

    - (void)someAction:(id)sender {
        // If the secondary window hasn't been loaded yet, load it and add its
        // window as a window attached to the main window
        if (self.secondaryWindowController == nil) {
            self.secondaryWindowController = [[[MySecondaryWindowController alloc]
                initWithWindowNibName:@"MySecondaryWindow"] autorelease];
            [[self window] addChildWindow:self.secondaryWindowController.window
                                  ordered:NSWindowAbove];
        }
    }
    

    If you want to add a view to the main window, the easiest way is to do so in the nib file directly.

    If you need/want to do it programatically, you need to have a reference to the view and then add it to the main window’s content view, e.g.:

    [self.window.contentView addSubView:someView];
    

    You can create someView programmatically or load it from a separate nib file. In the latter case, the procedure is much like what was described above but instead of using a subclass of NSWindowController you’d use a subclass of NSViewController.

    For example, in MyWindowController.m:

    - (void)anotherAction:(id)sender {
        // If the view hasn't been loaded yet, load it and add it
        // as a subview of the main window's content view
        if (self.someViewController == nil) {
            self.someViewController = [[[MyViewController alloc]
                initWithNibName:@"MyView" bundle:nil] autorelease];
            // You'll probably want to set the view's frame, e.g.
            // [self.someViewController.view setFrame:...];
            [self.window.contentView addSubview:self.someViewController.view];
        }
    }
    
    0 讨论(0)
提交回复
热议问题