【问题标题】:iPhone View methodsiPhone 查看方法
【发布时间】:2011-03-08 05:58:07
【问题描述】:

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

【问题讨论】:

    标签: iphone methods view viewdidload


    【解决方案1】:
    1. Init 或 InitWithCode 或 InitWithNibName 或(任何初始化方法)
    2. loadView ///它只在你有自定义viewController并且你从nibFile加载自定义视图时调用,通常不调用loadView
    3. viewDidLoad
    4. viewWillAppear
    5. viewDidAppear

    在卸载端

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

    这些特定于 viewController 仅不适用于 tableView 或其他类型的 viewController。如果我缺少任何方法,请添加 cmets。

    【讨论】:

    • 这些方法中的每一个都必须以正确的方式对super 进行相同的调用,否则您将陷入无限循环。创建或显示某些东西的方法首先调用 super,然后执行任何操作。删除视图的方法使对 super 的调用最后。例如,viewWillAppear:(BOOL)animated 的第一行应该是[super viewWillAppear:animate],而viewWillDisappear:(BOOL)animated 的最后一行应该是[super viewWillDisappear:animated]。后者就像在你自己的dealloc 方法的最后一行调用[super dealloc]
    • 让自己相信它们被调用的顺序的一种简单方法是在NSLog(@"will load")NSLog(@"did load")NSLog(@"will appear")、...每个,看看控制台是什么。
    • Thanx waqas.. Hw r u?谢谢你的意见。
    【解决方案2】:

    对于您的具体问题,您应该在 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.
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-07-26
      • 1970-01-01
      • 2012-01-18
      • 1970-01-01
      • 2011-07-02
      • 2012-04-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多