NSWindowController澄清理解 [英] NSWindowController clarification of understanding

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

问题描述

我在项目中使用了NSWindowController多次,并且觉得我对这个重要类背后的概念有一个(非常)粗略的掌握。我想做的这篇文章是澄清/更正我自己的理解,并希望帮助其他学习者得到第一步了解。这是我发现的最有用的概念,概述和最佳做法,而且通常缺少文档中的概览。这里是我对NSWindowController的采取(问题是以粗体散布):




  • NSWindowController(NSWC)子类在概念上存在nib,用作用户界面元素和它们控制/表示的模型对象之间的粘合剂。基本上,应用程序中的每个窗口都应该有自己的NSWC子类。

  • nib的文件所有者应该是NSWC子类。 即使对于MainMenu.xib应用程序也是如此?

  • NSWC 窗口属性应始终为链接到InterfaceBuilder中的NSWindow。

  • 您应该使用 [super initWithWindowNibName:] 覆盖'init'方法,你参考 [mycontroller window] 它会加载nib。 即使是在启动时打开,MainCache.xib窗口的NSWC也应该是这种情况吗?

  • NSWC不应该做太多

  • 它可以使用绑定来修改UI,或者充当表的委托等等,或者通过在观察到更改时主动更改UI元素,或者上述任意一个组合(您使用的元素似乎是品味问题,各方面都有利弊)。

  • NSWC可以在必要时创建其他NSWCs的实例(例如,打开一次性子窗口时)。

  • 使用 [mycontroller showWindow:nil] 在前面显示相关联的窗口。如果你想窗口显示为工作表,使用类似:

      NSWindowController * mycontroller = [[MyController alloc] init] ; 
    [NSApp beginSheet:[mycontroller window]
    modalForWindow:[self window]
    modalDelegate:self
    didEndSelector:@selector(didEndMySheet:returnCode:contextInfo :)
    contextInfo :nil];




didEndSelector: 应该是父窗口的NSWC的一个方法,并且可以使用 [sheet windowController] 访问和释放'mycontroller'。
- 关闭窗口调用NSWC窗口的 performClose:方法。



问题:




  • MainMenu窗口的NSWC是否应该是应用程式委托, / li>
  • 同样,如果主要NSWC处理文件(拖放和打开),或者应该传递给应用程序委托,还是只是一个问题? / li>


请纠正我,如果这是不好的做法,或者是纯粹的错误。我想要澄清对NSWindowController的理解,所以任何添加(以最佳实践,经验,陷阱的形式)将受到高度赞赏。



谢谢,
Laurie

解决方案

窗口控制器实际上是什么?



控制器是用于从NIB文件加载窗口并且用于管理在NIB中分配的资源的存储器的工具。之前, NSWindowControllers 一个基本上必须为每个窗口编写相同的代码或创建一个自己的窗口控制器类。



当然,它们也是模型/视图/控制器意义上的控制器,因此它们是将视图从窗口连接到模型对象的正确位置。为此,它们通常需要充当视图对象的委托或数据源。所以你得到这个部分完全正确。



还有窗口控制器是一个代码重用的工具。这使得很容易将窗口控制器类和它的XIB / NIB放到另一个项目中并在那里使用。



因此,是的,来自NIB的每个窗口都应该由一个窗口控制器,有一个例外。实际上,这只是一个良好代码的指南,没有什么强制它。



WindowControllers和MainMenu.xib



MainMenu.xib 是一个不同的东西,你不能使用窗口控制器。这个NIB由 NSApplication 加载,所以这必须是它的文件所有者。没有办法在 NSApplication 和NIB之间获得窗口控制器。它也没有必要使用窗口控制器进行内存管理,因为应用程序对象存在于程序的整个运行时,所以它不需要清除它的资源从NIB当它被释放时。



如果你真的需要一个窗口控制器的主窗口,你不能把它放在 MainMenu.xib 。 / p>

我希望这会有帮助。可能还有很多关于窗口控制器的东西。


I have used NSWindowController in projects several times, and feel like I have a (very)rough grasp of the concepts behind this important class. What I would like to do with this post is to clarify/correct my own understandings, and hopefully help other learners get that first step into understanding. It's the at-a-glance concepts, overview, and best practices that I find is most useful, and often lacking in the documentation. Here is my take on NSWindowController (questions are interspersed in bold):

  • An NSWindowController (NSWC) subclass exists (conceptually) just beneath every window nib, acting as the glue between the user interface elements and the model objects that they control/represent. Basically, every window in your application should have its own NSWC subclass.
  • The File's Owner of the nib should always be the NSWC subclass. Is this the case even for the MainMenu.xib application?
  • The NSWC window property should always be linked to the NSWindow in InterfaceBuilder.
  • You should override the 'init' method, using [super initWithWindowNibName:], so that when you refer to [mycontroller window] it will load the nib. Should this also be the case for the NSWC for the MainMenu.xib window, even though this is opened at startup?
  • The NSWC shouldn't do too much heavy lifting - it should simply pass messages to instances of objects, and present those objects in the UI.
  • It can modify the UI using binding, or acting as a delegate for tables etc., or by actively changing the UI elements when it observes a change, or a combo of any of the above (which one you use seems to be a matter of taste, with pros and cons on all sides).
  • An NSWC can create instances of other NSWCs when necessary (for example, when opening a one-off sub-window).
  • Use the [mycontroller showWindow:nil] to display the associated window at the front. If you want the window to appear as a sheet, use something like:

    NSWindowController* mycontroller = [[MyController alloc] init];
    [NSApp beginSheet: [mycontroller window]
       modalForWindow: [self window] 
        modalDelegate: self 
       didEndSelector: @selector(didEndMySheet:returnCode:contextInfo:)
          contextInfo: nil];
    

The didEndSelector: should be a method of the NSWC of the parent window, and can access and release 'mycontroller' with [sheet windowController]. - To close the window call the performClose: method of NSWC's window.

Some Questions:

  • Should the NSWC of the MainMenu window also be the application delegate, or should this be a different class?
  • In the same vein, should the main NSWC handle files (drag/drop and opening), or should it be passed on to the app delegate, or is that just a matter of taste?

Please correct me if any of this is bad practice, or is just plain wrong. I am looking to clarify my understanding of NSWindowController, so any additions (in the form of best practices, experiences, gotchas) would be highly appreciated.

Thanks, Laurie

解决方案

What are window controllers actually for?

Window controllers are tools to load a window from a NIB file and for managing the memory of the resources allocated in the NIB. Before there where NSWindowControllers one basically had to write the same code for every window or invent an own window controller class.

Of course they are also controllers in the Model/View/Controller sense, so they are the right place to connect the views from the window to the model objects. To do this they often need to act as the delegate or data source for a view object. So you got this part perfectly right.

Also window controllers are a tool for code reuse. It makes it easy to drop the window controller class and it’s XIB/NIB into another project and use it there.

So yes, every window from a NIB should be owned by a window controller, with one exception. Actually, this is just a guideline for good code, nothing enforces it.

WindowControllers and MainMenu.xib

MainMenu.xib is a different thing, there you can’t use a window controller. This NIB gets loaded by NSApplication so this has to be it’s "Files owner". There is no way to get a window controller between the NSApplication and the NIB. It also isn’t necessary to use a window controller for memory management there, since the application object lives for the entire runtime of the program, so it doesn’t have to clean up it’s resources from the NIB when it gets deallocated.

If you really need a window controller for your main window you cannot put this in the MainMenu.xib.

I hope this helps. There probably is a lot more to say about window controllers too

这篇关于NSWindowController澄清理解的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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