希望了解iOS UIViewController生命周期

你能解释一下管理UIViewController生命周期的正确方式吗?

特别是,我想知道如何在Mono Touch中为UIViewController类使用InitializeViewDidLoadViewWillAppearViewDidAppearViewWillDisappearViewDidDisappearViewDidUnloadDispose方法。


当您加载/呈现/隐藏视图控制器时,所有这些命令都会在iOS的适当时候自动调用。 注意这些方法连接到UIViewController而不连接到UIView本身是很重要的。 使用UIView不会获得任何这些功能。

苹果公司网站上有很棒的文档。 简单地说,虽然:

  • ViewDidLoad - 在创建类并从xib加载时调用。 非常适合初始设置和一次性工作。

  • ViewWillAppear - 在视图出现之前调用,适用于在视图可见之前隐藏/显示字段或每次希望发生的任何操作。 因为您可能会在视图之间来回切换,所以每当您的视图即将出现在屏幕上时都会调用此视图。

  • ViewDidAppear - 在视图出现后调用 - 用于启动动画或从API加载外部数据的好地方。

  • ViewWillDisappear / DidDisappear -相同的思路ViewWillAppear / ViewDidAppear

  • ViewDidUnload / ViewDidDispose - 在Objective C中,这是你清理和发布内容的地方,但是这是自动处理的,所以你并不需要在这里做很多事情。


  • UIViewController的生命周期在这里显示:

    http://rdkw.wordpress.com/2013/02/24/ios-uiviewcontroller-lifecycle/

    视图控制器的生命周期,图解


    这是最新的iOS版本(用Xcode 9.3,Swift 4.1修改)。 以下是使UIViewController的生命周期完成的所有阶段。

    的loadView()

    loadViewIfNeeded()

    viewDidLoad中()

    viewWillAppear(_ animated:Bool)

    viewWillLayoutSubviews()

    viewDidLayoutSubviews()

    viewDidAppear(_ animated:Bool)

    viewWillDisappear(_ animated:Bool)

    viewDidDisappear(_ animated:Bool)

    让我解释所有这些阶段。

    1. loadView

    此事件创建控制器管理的视图。 只有在以编程方式创建视图控制器时才会调用它。 这使它成为在代码中创建视图的好地方。

    This is where subclasses should create their custom view hierarchy if they aren't using a nib. 
    Should never be called directly. 
    

    2. loadViewIfNeeded()

    如果尚未设置当前视图viewController的视图,那么此方法将加载视图,但请记住,这仅在iOS> = 9.0中可用。 所以如果你支持iOS 9.0,那么不要期望它会出现在图片中。

    Loads the view controller's view if it has not already been set.
    

    3. viewDidLoad

    viewDidLoad事件只在视图创建并加载到内存中时才被调用,但视图的边界尚未定义。 这是初始化视图控制器将要使用的对象的好地方。

    Called after the view has been loaded. For view controllers created in code, this is after -loadView.
    For view controllers unarchived from a nib, this is after the view is set.
    

    4. viewWillAppear

    只要视图出现在屏幕上,该事件就会通知viewController 。 在这个步骤中,视图具有已定义的边界但未定位方向。

    Called when the view is about to made visible. Default does nothing.
    

    5. viewWillLayoutSubviews

    这是边界最终确定的生命周期中的第一步。 如果您未使用约束或自动布局,则可能需要在此处更新子视图。 这仅适用于iOS> = 5.0。 所以如果你支持iOS 5.0,那么不要期望它会出现在图片中。

    Called just before the view controller's view's layoutSubviews method is invoked.
    Subclasses can implement as necessary. The default is a nop.
    

    6. viewDidLayoutSubviews

    此事件通知视图控制器已设置子视图。 这是一个很好的地方,可以在设置后对子视图进行任何更改。 这仅适用于iOS> = 5.0。 所以如果你支持iOS 5.0,那么不要期望它会出现在图片中。

    Called just after the view controller's view's layoutSubviews method is invoked.
    Subclasses can implement as necessary. The default is a nop.
    

    7. viewDidAppear

    视图出现在屏幕上后, viewDidAppear事件触发。 这使得它成为从后端服务或数据库获取数据的好地方。

    Called when the view has been fully transitioned onto the screen.
    Default does nothing
    

    8. viewWillDisappear

    viewWillDisappear事件在呈现的viewController的视图即将消失,解散,覆盖或隐藏在其他viewController后面时viewController 。 这是一个很好的地方,您可以限制网络调用,使绑定到该viewController计时器或释放对象失效。

    Called when the view is dismissed, covered or otherwise hidden.
    

    9. viewDidDisappear

    这是任何人都可以解决的生命周期的最后一步,因为在呈现的viewController的视图消失,解散,覆盖或隐藏之后,此事件才会触发。

    Called after the view was dismissed, covered or otherwise hidden. 
    Default does nothing
    

    现在按照Apple在实施这种方法时,应该记得调用该特定方法的super实现。

    If you subclass UIViewController, you must call the super implementation of this
    method, even if you aren't using a NIB.  (As a convenience, the default init method will do this for you,
    and specify nil for both of this methods arguments.) In the specified NIB, the File's Owner proxy should
    have its class set to your view controller subclass, with the view outlet connected to the main view. If you
    invoke this method with a nil nib name, then this class' -loadView method will attempt to load a NIB whose
    name is the same as your view controller's class. If no such NIB in fact exists then you must either call
    -setView: before -view is invoked, or override the -loadView method to set up your views programatically.
    

    希望这有助于。 谢谢。

    更新 - 正如@ThomasW指出的内部注释viewWillLayoutSubviewsviewDidLayoutSubviews也将在加载主视图的子视图时调用,例如加载表视图或集合视图的单元格时。

    链接地址: http://www.djcxy.com/p/87739.html

    上一篇: Looking to understand the iOS UIViewController lifecycle

    下一篇: Custom init for UIViewController in Swift with interface setup in storyboard