zoukankan      html  css  js  c++  java
  • View Controllers(7)

    Setting a view controller as the rootViewController of a window adds that view controller’s view as a subview of the window (Figure 7.4). It also automatically resizes the view to be the same size as the window. Given what you learned in Chapter 6, you could write setRootViewController: yourself:

    - (void)setRootViewController:(UIViewController *)vc {

    // Get the view of the root view controller UIView *rootView = [vc view];

    // Make a frame that fits the window's bounds CGRect viewFrame = [self bounds];
    [rootView setFrame:viewFrame];

    // Insert this view as window's subview

    [self addSubview:rootView]; }

    (This method does are a few more things, but this is what we are interested in right now). 

    There is another type of object in a XIB file: placeholder objects. There are two objects in this XIB file in the Placeholder section: File's Owner and First Responder. You can safely ignore the First Responder, but the File's Owner is very important.

    To understand the File's Owner, you must first understand the need for it. When a view controller loads its view, it sets its view property so that it knows what its view is and can put it on the screen. For the HypnosisViewController, we did this programmatically, so it was done in loadView and all set at compile time.

    Not so with TimeViewController. When an instance of TimeViewController needs to load its view, it will load the XIB file. When this happens, all of the archived objects in the XIB will be created, and TimeViewController can’t know which of those objects is its view.

    Here’s where the File's Owner comes in: the File's Owner is a hole in the XIB file. You make connections between objects in the XIB and File's Owner when configuring the interface. When the XIB file is loaded, the TimeViewController drops itself in the File's Owner hole, and all the connections between the File's Owner and the archived objects will be made to the TimeViewController.

    To be able to set the connections that a TimeViewController needs, we have to tell Xcode that TimeViewController is the class of the object that will drop itself into the hole. Select the File's Owner object on the outline view and click the icon in the inspector area to show the identity inspector. Change the Class to TimeViewController (Figure 7.9). 

     A view controller can get its view two different ways:

    A view controller whose view doesn’t have any subviews will override loadView to create a view instance and send the message setView: to itself.
    A view controller with a more complex view hierarchy will load its
    view from a XIB file by connecting its view outlet to the top-level view archived in the XIB. 

    (实际上ios6开始,内存不足时不会去掉用viewDidUnload 方法了。)

    top级别的需要强引用,当你一个页面需要切换多个页面的时候,可定义多个top级别的view,在同一个xib文件中定义好,以强引用的方式进行view的切换,,必须以强引用(outlet)的方式进行view的add或remove等操作,弱引用的话,刚出生,就灭了,不信你试试。

    附上viewController的lifecycle

    The main Function and UIApplication 

    int main(int argc, char *argv[]) {

    @autoreleasepool {
    return UIApplicationMain(argc, argv,

    nil, NSStringFromClass([HypnoAppDelegate class]));

    } } 

     

    The function UIApplicationMain creates an instance of a class called UIApplication. For every application, there is a single UIApplication instance. This object is responsible for maintaining the run loop. Once the application object is created, its run loop essentially becomes an infinite loop: the executing thread will never return to main.

    Another thing the function UIApplicationMain does is create an instance of the class that will serve as the UIApplication’s delegate. Notice that the final argument to the UIApplicationMain function is an NSString that is the name of the delegate’s class. So, this function will create an instance of HypnoAppDelegate and set it as the delegate of the UIApplication object. (Where does the Hypno in HypnoAppDelegate come from? It’s what you entered for the class prefix when creating this project.)

    Right before the run loop begins accepting events, the application sends a message to its delegate saying, “Get ready because here we go!” This message is application:didFinishLaunchingWithOptions:. You implemented this method in HypnoAppDelegate.m to create the window and the controller objects used in this application. 

    (有空去了解一下run loop)

  • 相关阅读:
    领会一些比较巧妙的算法
    操作系统os常识
    C++中的继承与虚函数各种概念
    我学shell程序的记录
    matlab:linux环境中将m文件编译成动态链接库
    struct内存对齐:gcc与VC的差别
    fedora中丢失或损坏fstab,无法启动,如何补救
    判断一个字符串中的字符是否都在另一个中出现
    linux下的不错的小软件:apvlv,zathura和vifm
    C语言中将结构体写入文件
  • 原文地址:https://www.cnblogs.com/zhangjl/p/3626980.html
Copyright © 2011-2022 走看看