Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS Swift how to monitor UIViewController lifecyle

Tags:

ios

swift

I am new to iOS development. 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.

and viewDidLoad/viewWillAppear method to onCreate/onStart method

and viewDidAppear method to onResume method

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).

It means that now these methods will be triggered/called whenever there is any navigation from one screen to another in the android app.

How do I do this in iOS using Swift? How do I know which screen(UIViewcontroller) the user is currently in and where he is navigating?

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?

Someone please help me out.

Edit:- I don't want them to subclass my standalone class instead of UIViewController class.

like image 918
Nongthonbam Tonthoi Avatar asked Jul 13 '16 23:07

Nongthonbam Tonthoi


People also ask

What is the lifecycle of UIViewController?

The view controller lifecycle can be divided into two big phases: the view loading and the view lifecycle. The view controller creates its view the first time the view is accessed, loading it with all the data it requires. This process is the view loading.

Does UIView have viewDidLoad?

A UIView instance has a clearly defined lifecycle. Your application can hook into that lifecycle through a collection of methods, such as UIView 's layoutSubviews() and UIViewController 's viewDidLoad() , viewWillAppear(_:) , and viewWillDisappear(_:) .

What is the difference between viewDidLoad () and viewDidAppear ()?

The difference between viewDidAppear and viewDidLoad is that viewDidAppear is called every time you land on the screen while viewDidLoad is only called once which is when the app loads.

Is viewDidLoad called before viewWillAppear?

viewWillAppear(_:)Always called after viewDidLoad (for obvious reasons, if you think about it), and just before the view appears on the screen to the user, viewWillAppear is called.

What is UIViewController in Swift?

The UIViewController class defines the shared behavior that's common to all view controllers. You rarely create instances of the UIViewController class directly. Instead, you subclass UIViewController and add the methods and properties needed to manage the view controller's view hierarchy.

What is viewWillAppear in Swift?

Notifies the view controller that its view is about to be added to a view hierarchy.

When loadView is called?

The view controller calls this method when its view property is requested but is currently nil . This method loads or creates a view and assigns it to the view property. If the view controller has an associated nib file, this method loads the view from the nib file.

What is viewDidLoad in Swift?

This method is called after the view controller has loaded its view hierarchy into memory. This method is called regardless of whether the view hierarchy was loaded from a nib file or created programmatically in the loadView method.


1 Answers

There are no global events that are fired when the UIViewController lifecycle methods are called. To create those you would need to subclass UIViewController as has been suggested.

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).

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 -

  • viewDidLoad called after the view controller instance is loaded (once per instantiation)
  • viewWillAppear called before this view appears
  • viewDidAppear called after this view appears
  • viewWillDisappear called before this view disappears
  • viewDidDisappear called after this view disappears

enter image description here

like image 82
Paulw11 Avatar answered Oct 24 '22 20:10

Paulw11