2011-03-08 138 views
0

请逐步告诉我iOS的不同视图方法。请说明首先执行哪个方法等。我的意思是说什么时候执行哪个方法?iPhone查看方法

回答

0

对于您的具体问题,您应该在viewDidLoad中添加子视图。因为,如果你覆盖loadView,你必须完成所有的工作,加载所有的视图。

下面是从Apple's documentation:

负载循环过程中发生的步骤的说明如下:

1. 

    * Some part of your application asks for the view in the view 

controller’s view property. 

2. 

    * If the view is not currently in memory, the view controller calls its loadView 

method. 

3. 

    * The loadView method does one of the following: 

     If you override this method, your implementation is 

responsible for creating all necessary views and assigning a non-nil value to the view property. 

     If you do not override this method, the default implementation uses 

the nibName and nibBundle properties of the view controller to try to load the view from the specified nib file. If the specified nib file is not found, it looks for a nib file whose name matches the name of the view controller class and loads that file. 

     If no nib file is available, the method creates an empty UIView object 

and assigns it to the view property. 

4. 

    * The view controller calls its viewDidLoad method to perform any 

additional load-time tasks. 
3
  1. 初始化或InitWithCode或InitWithNibName或(任何初始化方法)
  2. 的loadView ///当你有自定义的viewController和加载自定义视图,而不是从nibFile,正常的loadView不叫,只叫
  3. viewDidLoad中
  4. viewWillAppear中
  5. viewDidAppear

上卸载侧

  1. viewWillDisappear
  2. viewDidDisappear
  3. viewDidUnload
  4. 的dealloc

这些特定于viewController只适用于tableView或其他类型的viewController。如果我缺少任何方法PLZ添加评论。

+1

这些方法都必须进行相同的调用,以'super'并以正确的方式,否则你会在一个无限循环。创建或显示某些东西的方法先超级,然后做任何事情。删除视图的方法使超级最后的调用成为可能。例如,'viewWillAppear:(BOOL)animated'的第一行应该是'[super viewWillAppear:animate]',而'viewWillDisappear:(BOOL)animated'的最后一行需要是'[super viewWillDisappear:animated]'' 。后者就像在你自己的'dealloc'方法的最后一行调用'[super dealloc]'。 – SK9 2011-03-08 06:14:24

+1

一个简单的方法来说服他们是所谓的顺序自己是做'的NSLog(@“将加载”)','的NSLog(@“做负载”)','的NSLog(@“将出现”) ',...,的NSLog在每个(@“的dealloc”),看看从控制台是什么。 – SK9 2011-03-08 06:15:27

+0

Thanx waqas .. Hw r u? thnx为你的观点。 – 2011-03-08 10:23:09