- viewDidLoad 和 - viewWillApear 和 -viewDidAppear

文字参考:stackoverflow.com ios

In general, this is what I do: web

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.) app

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).) dom

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.) iphone


============================================================== ide

文字来源:UIViewController类参考 动画


- (void)viewDidLoad ui

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

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.

相关文章
相关标签/搜索