UIViewController读书笔记

  1. 当一个VC把另一个VC作为子view加到自己的view时,一定要先调用addChildViewController(_:)方法。
    因为一个VC的root view,也就是VC的view只能被这个VC持有。addSubView时,子VC的view会被父VC持有,违法了这个原则。所以要先建立两个VC之间的父子关系。

    Each view controller object is the sole owner of its view. You must not associate the same view object with multiple view controller objects. The only exception to this rule is that a container view controller implementation may add this view as a subview in its own view hierarchy. Before adding the subview, the container must first call its addChildViewController(_:) method to create a parent-child relationship between the two view controller objects.

    UIViewControllerHierarchyInconsistencyException

    When a view controller'€™s view is added to the view hierarchy, the system walks up the view hierarchy to find the first parent view that has a view controller. That view controller must be the parent of the view controller whose view is being added. Otherwise, this exception is raised.

    就是说A调用addsubView将B作为子view以后,系统会检查B view的父View A对应的VC是不是这个view B的VC的父VC。如果不是,就会产生这个异常。

  2. 判断一个VC的view是否被加载
    获取一个VC的view时,会调用这个VC的loadView方法。可以使用isViewLoaded()方法来避免加载这个view.

    Because accessing this property can cause the view to be loaded automatically, you can use the isViewLoaded() method to determine if the view is currently in memory. Unlike this property, the isViewLoaded() property does not force the loading of the view if it is not currently in memory.

  3. viewDidLoad()的调用时机
    loadView()之后,一定会被调用。如果指定了VC的xib文件,这个方法来进一步做初始化。 如果纯手写代码,感觉和loadView()差别不大。

posted on 2017-01-14 10:18  花老🐯  阅读(135)  评论(0编辑  收藏  举报

导航