(Cocoa)在单个窗口的可可应用程序中相当于一个UIViewController子类? [英] (Cocoa) What's the equivalent of a UIViewController subclass in a single window cocoa app?

查看:121
本文介绍了(Cocoa)在单个窗口的可可应用程序中相当于一个UIViewController子类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在讨论iOS应用程序后,我想我会给可可应用程序一个旋转,假设事情会非常相似。所以我有一个单窗口应用程序与一个拆分视图和一些 NSTableView (考虑iTunes),我正在寻找一个地方,我的代码,从数据提取



在iOS中,我将它放在适当的<$ c>的 viewDidLoad $ c> UIViewController 子类。 UITableViewDataSource 将访问此数据以填充其单元格。



现在我有一个 NSWindow ,我可以做一个类似的事情,但如何让一个 NSWindowController 为它?这是甚至我想要的?我可以把它放在 AppDelegate 中,但似乎不对。

解决方案

p> iOS和OS X之间的区别是,在iOS中只有一个窗口,但在OS X中可以有多个窗口。 NSViewControllers 在OS X中的工作方式不同于 UIViewControllers ,因为 NSViewController 旨在加载和处理单个视图,而 UIViewControllers (对我相对缺乏iOS知识的歉意)似乎处理多个视图,并提供更多的视图之间的粘合。



即使 Apple docs 写道:


在Mac OS X中,AppKit视图控制器是窗口的助手
控制器,它最终负责在窗口中的所有


因此, UIViewController 在OS X中不是 NSViewController ,而是 NSWindowController 通过管理单个窗口的整体和其中的视图的布局/内容/交互,为OS X提供了大部分 UIViewController



在你的情况下,我会使用 NSWindowController - 虽然如果应用程序很简单, App Delegate 工作太;如果应用程序非常复杂,那么使用 NSViewController 分割代码不会是一个坏主意。



使用 NSWindowController 的最佳方法是使用 [[CustomWindowController alloc] init]

  @implementation CustomWindowController 
- (id)init
{
self = [super initWithWindowNibName:@CustomWindowNibName];
if(self)
{
//执行任何初始化
}
return self;
}
@end

或致电

  [[CustomWindowController alloc] initWithWindowNibName:@CustomWindowNibName]; 


如果您希望它可重用,则直接覆盖initWithWindowNibName b

您可以删除MainMenu.xib中的默认窗口。






从根本上说,但是,NSWindowController管理在其自己的nib文件中实例化的窗口。 NSWindowController通常拥有该nib文件。 (虽然可以让它管理一个程序化创建的窗口,这通常不是如何完成的。)



为了能够使用自定义 NSWindowController ,因此需要在单独的nib / xib文件中使您的窗口被管理。 (使用默认的xib文件意味着你允许Cocoa自动实例化一个NSWindowController而没有子类化的机会;你不能在默认的NSMainNibFile中使用自定义的 NSWindowController 只需将所有控制器代码放在NSApplication / App Delegate中)



至少在Xcode 4中,该过程涉及使用窗口模板和自定义 NSWindowController 类,基于那个nib在-init或任何位置实例化 CustomWindowController 类,然后调用 [CustomWindowController showWindow:self]; (或者如果这不工作, -makeKeyAndOrderFront:)得到窗口实际显示$ c> - (void)applicationDid / WillFinishLaunching:(NSNotification *)aNotification 可能是一个不错的地方。)



要停止默认窗口从显示我只是删除它。可能有更好的方法,但我不知道。


After messing around with iOS apps, I thought I'd give a Cocoa app a whirl assuming things would be very similar. So I have a single window app with a split view and some NSTableViews (think iTunes) and I'm looking for a place to put my code that will fetch data from the web to fill the tables.

In iOS I would put this in the viewDidLoad method of the appropriate UIViewController subclass. The UITableViewDataSource would then access this data to populate its cells.

Now I have an NSWindow for which I could do a similar thing but how do I make a NSWindowController for it? Is this even what I want? I could put it in the AppDelegate but doesn't seem right.

解决方案

The difference between iOS and OS X is that there's only one window in iOS but there can be multiple in OS X. NSViewControllers in OS X work differently than UIViewControllersin that the NSViewController is designed to load and handle a single view, while UIViewControllers (apologies for my relative lack of iOS knowledge) seem to handle multiple views and provide much more of the glue between views.

Even the Apple docs write that:

In Mac OS X, AppKit view controllers are assistants to the window controller, which is ultimately responsible for everything that goes in the window.

Hence, the counterpart for UIViewController in OS X isn't NSViewController, but rather NSWindowController, which does provide for OS X much of what UIViewController does for iOS by managing the entirety of an individual window and the layout/content/interaction of the views within.

In your case, I would use an NSWindowController - though if the app is very simple, the App Delegate works too; and if the app is very complex, then using a NSViewController to split up the code wouldn't be a bad idea.

The best way to do use an NSWindowController would be to programatically load it in the App Delegate using [[CustomWindowController alloc] init] and

@implementation CustomWindowController
- (id)init
{
    self=[super initWithWindowNibName:@"CustomWindowNibName"];
    if(self)
    {
        //perform any initializations
    }
    return self;
}
@end

Or calling

[[CustomWindowController alloc] initWithWindowNibName:@"CustomWindowNibName"];

directly (and overriding initWithWindowNibName) if you want it to be reusable.

And you can delete the default window in MainMenu.xib.


Fundamentally, more often than not, an NSWindowController manages a window instantiated in its own nib file. The NSWindowController usually owns that nib file. (Though it is possible to have it manage a programmatically created window, that isn't usually how it's done.)

To be able to use a custom NSWindowController, you therefore need to make your window-to-be-managed in a separate nib/xib file. (using the default xib file means that you allow Cocoa to automatically instantiate a NSWindowController without opportunity for subclassing; you cannot use a custom NSWindowController with the default NSMainNibFile. For a simple app, just put all the controller code in the NSApplication/App Delegate)

In Xcode 4 at least, the process involves creating a xib with the window template and the custom NSWindowController class, instantiating the CustomWindowController class based on that nib in -init or wherever and then calling [CustomWindowController showWindow:self]; (or if that doesn't work, -makeKeyAndOrderFront:) to get the window to actually show (in - (void)applicationDid/WillFinishLaunching:(NSNotification *)aNotification might be a nice place).

To stop the default window from showing I just delete it. There's probably a better way but I don't know that.

这篇关于(Cocoa)在单个窗口的可可应用程序中相当于一个UIViewController子类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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