交换视图 - NSWindowController和NSViewController [英] Swapping views - NSWindowController and NSViewController(s)

查看:541
本文介绍了交换视图 - NSWindowController和NSViewController的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Mac OS编程中的新手。目前,我试图创建简单的测量应用程序,将有一个窗口的工具栏在顶部和适当的视图在底部。点击工具栏中的按钮应该会导致切换视图下面 - 例如。点击连接按钮将显示连接设置,测量将显示设备的当前数据。

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.

问题是 - 我不知道如何句柄交换视图,也许换句话说 - 我知道,但不完全...
我发现类似的讨论在这里: NSViewController和来自Nib的多个子视图,但没有答案如何创建 NSWindowController 以及如何将其分配给主窗口。因为我想有必要创建NSWindowController能够交换视图。如果我错了,请更正我。

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.

所以我正在创建一个新项目(这里叫Sample),有SampleAppDelegate.h文件,如下所示: / p>

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

有窗口ivar,只保留一个窗口

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

那么我该如何从SampleAppDelegate创建窗口的NSWindowController?

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

我应该创建我的WindowController子类,并在SampleAppDelegate的函数
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
中,如下所示:

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

推荐答案

您不应该需要NSWindowController视图交换,NSWindowController使用(我想)只是当你需要多个toplevel窗口。

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

您可以为每种类型的视图子类化NSViewController,将每个视图放到一个nib中,并在需要视图时调用 - (NSView *放入窗口的底部。你应该能像正常一样将它添加到窗口,或者通过使用setContentView将它放在NSBox:view

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

对于你的两个视图,你可以创建MeasurmentsViewController和一个ConnectionViewController。然后在MeasurementsView.nib和ConnectionView.nib中创建视图,并使用这些nibs来初始化视图控制器。

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.

然后在主窗口中,放置一个NSBox,如果你想把MeasurementsView放到它

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];

并将ConnectionView放入其中

and to put the ConnectionView into it

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

这篇关于交换视图 - NSWindowController和NSViewController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆