简体   繁体   English

iOS Swift 如何监控 UIViewController 生命周期

[英]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 methodviewDidLoad/viewWillAppear方法到onCreate/onStart方法

and viewDidAppear method to onResume methodviewDidAppear方法到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我们可以监视通过实施其中的这些方法(包括其他生命周期方法)被触发/被叫InterfaceActivityLifecycleCallbacks中存在的)(有点类似于在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.

相关问题 iOS Swift:如何在UIViewController中隐藏和显示按钮 - iOS Swift: How to hide and reveal a button in UIViewController 如何将PFQueryTableViewController放入UIViewController iOS Swift - How to Put PFQueryTableViewController Inside UIViewController iOS Swift 如何从 iOS Swift 中的 xib UIViewController 启动? - How to initiate from xib UIViewController in iOS Swift? iOS Swift UIViewController旋转 - iOS Swift UIViewController Rotation iOS / swift - swift中UIViewController中的常量 - iOS / swift - constants in UIViewController in swift 如何监视UIViewController删除UITouch? - How to monitor a UIViewController for the removal of a UITouch? 如何通过UIViewController(Swift,iOS 9)检测屏幕活动 - How to detect screen activity via UIViewController (Swift, iOS 9) ios - 如何在iOS Swift 3中以编程方式推送和呈现给UIViewController而无需segue - How to push and present to UIViewController programmatically without segue in iOS Swift 3 如何以编程方式从 iOS Swift 3 - How to push and present to UIViewController programmatically From a XIB in iOS Swift 3 如何将一个UIViewController类型传递给一个函数并在iOS Swift中输入一个变量? - How to pass a Type of UIViewController to a function and type cast a variable in iOS Swift?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM