• Placeholder Objects(File’s Owner,First Responder,Application)


    There are two kinds of objects displayed in the Interface Builder dock: Interface objects and placeholder objects. Interface objects are the objects that are actually created when the nib file is loaded and typically comprise the bulk of the objects. Placeholder objects refer to files that live outside of the nib file but which are intimately tied to the contents of the nib file. There are normally three placeholder objects in a nib file:

    1. File’s Owner
    2. First responder
    3. Application

    File’s Owner

    对于这个对象,大家争论不休,搞不清楚这个对象到底是个啥,我在网上看到的一个帖子说,搞懂File’s Owner,作为今年最大的生日礼物,看了跟帖也不知道他到底有没有收到生日礼物,不过我们还是要来看看的。

    File’s Owner Represents the nib file’s controller object. File’s Owner is the most commonly used placeholder object in nib files and is supported by both Mac OS X and iOS nib files. The File’s Owner placeholder is the main bridge between your application and the contents of the nib file.

    这里提到了一个词:bridge。我们应该看看bridge了什么信息

    When you build and run your application, the nib-loading code substitutes the object that is the file’s owner for any references to the File’s Owner placeholder in the nib file. This substitution results in the outlets and actions connected to the File’s Owner placeholder being connected to the object that is the file’s owner.

    当编译运行程序时,nib-loading的代码将nib文件中所有指向File’s Owner placeholder 的引用代替为file’s owner object。这个替换导致了原本连接到File’s Owner placeholder的outlet和action也会被连接到file’s owner对象中。

    You can designate any object in your application as the File’s Owner of a nib file. You tell Interface Builder the class of the File’s Owner so it knows what connections to make available. Typically, the file’s owner is a controller class that manages the interactions with the views and other controller objects inside the nib file.

    开发者可以指定应用程序中的任何对象为一个nib文件的File’s Owner 。开发者告诉Interface Builder File’s Owner 类名,从而Interface Builder知道让什么样的connections可用。通常来说,file’s owner 是一个Controller类,用来管理与view的交互,以及在nib中的其他的Controller 对象。

    如何在IB中配置File’s Owner

    Select the File’s Owner placeholder object in the Interface Builder dock

    1. Open the Identity inspector.
    2. In the Class field of the Custom Class section, set the class of the File’s Owner to the corresponding class in your application.
    3. The following table lists some of the standard classes that are commonly used to represent File’s Owner in applications:
      1. NSDocument
        1. only for Mac OS X
        2. Document-based applications store the document window and other required interface objects in a nib file. The File’s Owner of this nib file is traditionally the document object itself.
      2. NSWindowController
        1. only for Mac OS X
        2. Window controllers provide a great deal of automatic management for nib files and are especially useful when your nib file contains only one window.
      3. NSViewController
        1. only for Mac OS X
        2. A view controller manages custom accessory views and other view-based content.
      4. UIViewController
        1. only for iOS 
        2. A custom UIViewController object is often used to manage the content view for a distinct screen’s worth of content. A UI view controller provides automatic nib-loading and purging support.
      5. <Any custom NSObject subclass>
        1. only for Mac OS X
        2. You can use practically any object you like for manual control of a nib file. It is up to you to define the relationships between this class and the objects in your nib file.
    4. Control-click the File’s Owner object to see the outlets and actions defined by that class.

     

     如同前面介绍的,File’s Owner 是placeholder object,它不是加载nib时创建的,而是在nib创建完成后,set进去(substitute)。那File’s Owner对象是什么时候创建的,又是谁创建的呢?我创建了一个简单的Document-Based application,然后在init和awakeFromNib中设置断点,可以看到,当我在Main Windows中点击了New后,一个新的NSDocument被创建出来,并调用了init;在awakeFromNib里的断点被hit时,往上追踪,可以看到是NSDocument的showWindows调用loadNib,然后调用awakeFromNib。那NSDocument又知道应该load哪个NIB呢?

    又做了一个实验,我创建了一个只有一个Main Window的应用,这个Main Window的NSApplication,同样在init和awakeFromNib中设置断点,发现是loadNib调用了NSApplication的init函数,然后调用了awakeFromNib函数。这让我更加糊涂,他们的顺序关系,但不变的先创建File’s Owner object,然后才是awakeFromNib,并做关联。

    还有就是,我在RaiseMan程序中,由于使用NSArrayController做为MVC的C,并将Content Array绑定到了File's Owner的employees,我把窗口的File's Owner改变成另一个不相干的类,这时,应该报错吧!可是没有,一切运行正常!!

    对于他们之间如何建立联系,又如何运作的,只能以后慢慢研究了。

    First responder

    In a nib file, the First Responder is a placeholder object that represents the first object in your application’s dynamically determined responder chain. Because the responder chain of an application cannot be determined at design time, the First Responder placeholder acts as a stand-in target for any action messages that need to be directed at the application’s responder chain. Menu items commonly target the First Responder placeholder. For example, the Minimize menu item in the Window menu hides the frontmost window in an application, not just a specific window, and the Copy menu item should copy the current selection, not just the selection of a single control or view. Other objects in your application can target the First Responder as well.

    When you load a nib file into memory, there is nothing you have to do to manage or replace the First Responder placeholder object. The AppKit and UIKit frameworks automatically set and maintain the first responder based on the application’s current configuration.

    要完全理解First Responder需要搞懂Responder Chain和Event-Handling in Cocoa。 之后再看。


    Application

    In Mac OS X nib files, the Application placeholder object gives you a way to connect the outlets of your application’s shared NSApplication object to custom objects in your nib file. The default application object has outlets for its delegate object and, in Mac OS X applications, the application menu bar. If you define a custom subclass of NSApplication, you can connect to any additional outlets and actions defined in your subclass.

    At load time, the nib-loading code automatically replaces the Application placeholder object with the shared application object from your application.

    这个placeholder对象用于连接"application’s shared NSApplication object"的outlet到nib文件中的custom对象。现在还没用到,先不说太多。

  • 相关阅读:
    一次网络IO优化的讨论
    服务器框架回顾
    一个小工具:DebugFile
    TPO-23 C2 Advice on choosing courses
    TPO-23 C1 Post a student announcement
    TPO-22 C2 Revise a music history paper
    TPO-22 C1 Complain about a biased article
    TPO-21 C2 Which elective courses to take
    TPO-20-Apply for the undergraduate research fund
    TPO-21 C1 Find a building for orientation
  • 原文地址:https://www.cnblogs.com/whyandinside/p/2961325.html
Copyright © 2020-2023  润新知