• 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)

  • 相关阅读:
    JS基础的入口函数以及函数
    JS基础的数组
    JS基础的数学函数、日期时间函数、运算符和流程语句
    JS基础的引入、注释、变量、数据类型和输出语法
    CSS3的动画、弹性盒子和多媒体查询
    CSS3的颜色(RGBA)透明度、渐变颜色、图片、旋转和过渡
    CSS3边框、背景和字体
    java基础(九)---继续聊聊map
    java基础(八)---map
    java基础(七)--- set
  • 原文地址:https://www.cnblogs.com/zhangjl/p/3626980.html
Copyright © 2020-2023  润新知