交换视图 - NSWindowController 和 NSViewController
我对 Mac OS 编程很陌生。目前,我正在尝试创建简单的测量应用程序,该应用程序将有一个窗口,工具栏位于顶部,相应的视图位于底部。单击工具栏中的按钮应会导致其下方的视图切换 - 例如,单击“连接”按钮将显示连接设置,“测量”将显示设备的当前数据。
问题是 - 我不知道如何处理交换视图,也许换句话说 - 我知道但不完全正确的东西...... 我在这里找到了类似的讨论: NSViewController and multiple subviews from a Nib 但是没有答案如何创建 NSWindowController 以及如何将其分配给主窗口。因为我想有必要创建 NSWindowController 才能交换视图。如果我错了,请纠正我。
因此,我正在创建新项目(此处称为 Sample),并且有 SampleAppDelegate.h 文件,如下所示:
@interface SampleAppDelegate : NSObject <NSApplicationDelegate> {
@private
NSWindow *window;
}
@property (assign) IBOutlet NSWindow *window;
@end
有窗口 ivar,它包含从 MainMenu.xib 创建的唯一一个窗口(如我所想)。
那么我应该如何从 SampleAppDelegate 为窗口创建 NSWindowController 呢?
我应该只创建我的 WindowController 子类并在函数中吗 - (void)applicationDidFinishLaunching:(NSNotification *)aNotification SampleAppDelegate 像这样:
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
MyWindowController *wc = [[MyWindowController alloc] initWithWindow:self.window];
[wc showWindow:self];
self.myWindowController = wc;
[wc release];
}
我将非常感谢任何提示和帮助。
马尔钦
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不应该需要 NSWindowController 来进行视图交换,NSWindowController (我认为)仅在您需要多个顶级窗口时使用。
您可以为每种类型的视图创建 NSViewController 子类,将每个视图放入笔尖中,然后在需要将视图放入窗口底部时调用 -(NSView *)view 。您应该能够像平常一样将其添加到窗口中,或者使用 setContentView:view 将其放入 NSBox 中。
对于您的两个视图,您将创建 MeasurmentsViewController 和 ConnectionViewController。然后,您可以在MeasurementsView.nib 和ConnectionView.nib 中创建视图,并使用这些nib 来初始化视图控制器。
然后在你的主窗口中,如果你要放置一个 NSBox,如果你想将MeasurementsView 放入其中
并将 ConnectionView 放入其中
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
and to put the ConnectionView into it