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.
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.
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(_:) .
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.
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.
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.
Notifies the view controller that its view is about to be added to a view hierarchy.
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.
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.
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 appearsviewDidAppear
called after this view appearsviewWillDisappear
called before this view disappearsviewDidDisappear
called after this view disappearsIf you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With