[英]iOS Swift how to monitor UIViewController lifecyle
I am new to iOS development.我是 iOS 开发的新手。 So pardon me if this is a very basic thing.如果这是一个非常基本的事情,请原谅我。
From what I have learnt till now:从我学到的东西到现在:
UIViewController
class resembles somewhat equivalent to an Activity
class in Android. UIViewController
类有点类似于 Android 中的Activity
类。
and viewDidLoad/viewWillAppear
method to onCreate/onStart
method和viewDidLoad/viewWillAppear
方法到onCreate/onStart
方法
and viewDidAppear
method to onResume
method和viewDidAppear
方法到onResume
方法
Please correct me if I am wrong here.如果我在这里错了,请纠正我。
Now, in Android
we can monitor which of these methods(including other lifecycle methods) are triggered/called by implementing an Interface
( ActivityLifecycleCallbacks
) (somewhat resembling a protocol in iOS) which exists in the Application
class in any Activity (particularly in a class which extends Application class).现在,在Android
我们可以监视通过实施其中的这些方法(包括其他生命周期方法)被触发/被叫Interface
( ActivityLifecycleCallbacks
中存在的)(有点类似于在IOS的协议) Application
类的任何活动(特别是在类它扩展了 Application 类)。
It means that now these methods will be triggered/called whenever there is any navigation from one screen to another in the android app.这意味着现在只要在 android 应用程序中有从一个屏幕到另一个屏幕的任何导航,就会触发/调用这些方法。
How do I do this in iOS using Swift?如何使用 Swift 在 iOS 中执行此操作? How do I know which screen(UIViewcontroller) the user is currently in and where he is navigating?我如何知道用户当前所在的屏幕(UIViewcontroller)以及他正在导航的位置?
In short I want to write a standalone class which logs which screen(UIViewController) the user is currently in and which one of the lifecycle methods(viewDidLoad, viewWillAppear etc) is being executed?简而言之,我想编写一个独立的类来记录用户当前所在的屏幕(UIViewController)以及正在执行哪个生命周期方法(viewDidLoad、viewWillAppear 等)?
Someone please help me out.有人请帮帮我。
Edit:- I don't want them to subclass my standalone class instead of UIViewController class.编辑:- 我不希望他们继承我的独立类而不是 UIViewController 类。
There are no global events that are fired when the UIViewController
lifecycle methods are called.调用UIViewController
生命周期方法时不会触发全局事件。 To create those you would need to subclass UIViewController as has been suggested.要创建这些,您需要按照建议对 UIViewController 进行子类化。
You can look at UIApplication.sharedApplication().keyWindow?.rootViewController
to get the current root view controller (which is often, but not always the currently active view controller).您可以查看UIApplication.sharedApplication().keyWindow?.rootViewController
以获取当前的根视图控制器(通常但不总是当前活动的视图控制器)。
You wouldn't typically design an app that depended on some central object tracking the state of view controllers.您通常不会设计一个依赖于跟踪视图控制器状态的中心对象的应用程序。
The flow of UIViewController methods is pretty well described in the class reference and you can also work it out from the function names - UIViewController 方法的流程在类参考中有很好的描述,您也可以从函数名称中计算出来 -
viewDidLoad
called after the view controller instance is loaded (once per instantiation) viewDidLoad
在视图控制器实例加载后调用(每个实例化一次)viewWillAppear
called before this view appears viewWillAppear
在此视图出现之前调用viewDidAppear
called after this view appears viewDidAppear
在此视图出现后调用viewWillDisappear
called before this view disappears viewWillDisappear
在此视图消失之前调用viewDidDisappear
called after this view disappears viewDidDisappear
在此视图消失后viewDidDisappear
Create a view controller subclass and add that implementation in there.创建一个视图控制器子类并在其中添加该实现。 Then make sure all the view controllers you create subclass that new class rather than UIViewController itself然后确保您创建的所有视图控制器都是该新类的子类,而不是 UIViewController 本身
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.