Swapping views - NSWindowController and NSViewController(s)

做~自己de王妃 提交于 2019-12-24 07:38:01

问题


I'm very new in Mac OS programming. At the moment I'm trying to create simple measurement application which will have one window with the toolbar at the top and the appropriate view in the bottom. Clicking button in the toolbar should result in switching view below it - e.g. clicking on the "Connection" button will show with connection settings, "Measurements" will show current data from the device.

The problem is - I don't know how to handle swapping views, maybe in other words - something I know but not exactly... I found similar discussion here: NSViewController and multiple subviews from a Nib but there is no answer how to create NSWindowController and how to assign it to the Main window. Because I guess it is necessary to create NSWindowController to be able to swapping views. If I'm wrong, please correct me.

So I'm creating new project (called Sample here) and there is SampleAppDelegate.h file, which looks like:

@interface SampleAppDelegate : NSObject <NSApplicationDelegate> {
@private
    NSWindow *window;
}

@property (assign) IBOutlet NSWindow *window;

@end

There is window ivar, which holds the only one window, created from the MainMenu.xib (as I think).

So how should I create NSWindowController for the window from the SampleAppDelegate?

Should I just create my WindowController subclass and in the function - (void)applicationDidFinishLaunching:(NSNotification *)aNotification of the SampleAppDelegate like this:

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    MyWindowController *wc = [[MyWindowController alloc] initWithWindow:self.window];

    [wc showWindow:self];
    self.myWindowController = wc;
    [wc release];

}

I'll be very grateful for any hints and help.

Marcin


回答1:


You shouldn't need an NSWindowController to do view swapping, NSWindowController used (I think) just when you need multiple toplevel windows.

You can just subclass NSViewController for each type of view that you want, put each view into a nib, and call -(NSView *)view when you need a view to put into the bottom part of the window. You should be able to just add it to the window like normal, or put it in an NSBox by using setContentView:view

For your two views you'd create MeasurmentsViewController and a ConnectionViewController. Then you'd create your views in MeasurementsView.nib and ConnectionView.nib, and use those nibs to initialise your view controllers.

Then in your main window, if you were to put an NSBox, if you wanted to put the MeasurementsView into it

NSView *measurementsView = [measurementsViewController view];
[boxAtBottomOfWindow setContentView:measurementsView];

and to put the ConnectionView into it

NSView *connectionView = [connectionViewController view];
[boxAtBottomOfWindow setContentView:connectionView];


来源:https://stackoverflow.com/questions/6615859/swapping-views-nswindowcontroller-and-nsviewcontrollers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!