- viewDidLoad 和 - viewWillApear 和 -viewDidAppear

目录:[ - ]

  • Discussion
  • - (void)viewDidAppear:(BOOL) animated 该方法通知视图控制器,它的视图已经被加入视图树 Discussion 我们可以重写该方法执行附加任务,实现如何显示出视图。重写必须调用 super 方法。 Note: If a view controller is presented by a view controller inside of a popover, this method is not invoked on the presenting view controller after the presented controller is dismissed.
  • 该方法通知视图控制器,它的视图已经被加入视图树
  • Discussion
  • 文字参考:stackoverflow.com

    In general, this is what I do:

    1) ViewDidLoad - 需要的视图元素都在此方法加载,例如视图是一个Form,有3个label,这个方法一次成型(Whenever I'm adding controls to a view that should appear together with the view, right away, I put it in the ViewDidLoad method. Basically this method is called whenever the view was loaded into memory. So for example, if my view is a form with 3 labels, I would add the labels here; the view will never exist without those forms.)

    2) ViewWillAppear: 这里通常不做视图的修改,而用来更新Form数据,就是给给form重新填入新数据,该页面从别的页面回来时更新。UIView的创建非常费时费力,因此到 viewWillAppear时,iphone已经蓄势待发地、兴冲冲地要去显示了,就不要在这个地方再干费时费力的事情了,纯更新数据就可以了。(I use ViewWillAppear usually just to update the data on the form. So, for the example above, I would use this to actually load the data from my domain into the form. Creation of UIViews is fairly expensive, and you should avoid as much as possible doing that on the ViewWillAppear method, becuase when this gets called, it means that the iPhone is already ready to show the UIView to the user, and anything heavy you do here will impact performance in a very visible manner (like animations being delayed, etc).)

    3) ViewDidAppear: 最后,视图已经显示了,那这里可以做一些远程取数据的费时费力的工作。(Finally, I use the ViewDidAppear to start off new threads to things that would take a long time to execute, like for example doing a webservice call to get extra data for the form above.The good thing is that because the view already exists and is being displayed to the user, you can show a nice "Waiting" message to the user while you get the data.)


    ==============================================================

    文字来源:UIViewController类参考


    - (void)viewDidLoad

    该方法在控制器的视图载入内存时调用

    Discussion

    该方法在视图控制器(XXXViewController)已经将其视图树(视图里应有的各种东西)(view hierarchy)载入内存后调用。该方法的调用不考虑是否视图树是从nib文件(或storyboard)得来,还是纯代码获得;纯代码一般使用 loadView 方法,我们一般通过重写 loadView 方法做附加初始化,附加是指基于已有的 nib 文件或 storyboard。


    - (void)viewWillAppear:(BOOL)animated

    该方法通知视图控制器,它的视图将要(is about to) 被加入到视图树。

    Discussion

    该方法两个事件之前调用,一个是在视图将要被加入到视图树,二个是任何所需要的动画配置之前。我们可以重写该方法,实现定制如何显示视图,比如,使用该方法改变状态条(status bar)的方向和样式,以适应该视图启动时的方向和样式。重写该方法必须调用 [super viewWillAppear:animated]。.

    For more information about the how views are added to view hierarchies by a view controller, and the sequence of messages that occur, see “Responding to Display-Related Notifications”.

    Note: If a view controller is presented by a view controller inside of a popover, this method is not invoked on the presenting view controller after the presented controller is dismissed.


    - (void)viewDidAppear:(BOOL)  animated

    该方法通知视图控制器,它的视图已经被加入视图树

    Discussion

    我们可以重写该方法执行附加任务,实现如何显示出视图。重写必须调用 super 方法。

    Note: If a view controller is presented by a view controller inside of a popover, this method is not invoked on the presenting view controller after the presented controller is dismissed.

    你可能感兴趣的:(- viewDidLoad 和 - viewWillApear 和 -viewDidAppear)