Search Your Question

Showing posts with label Life Cycle. Show all posts
Showing posts with label Life Cycle. Show all posts

How many times Viewdidload and viewDidLayoutSubviews called?

Ans : 

ViewDidLoad called once only when all views are loaded.

viewDidLayoutSubviews :  Apple gave a very good explanation on this by saying that it is called to notify the view controller that its view has just laid out its subviews.

In another word, viewDidLayoutSubviews is called every time the view is updated, rotated or changed or it’s bounds change.

But know that with viewDidLayoutSubviews, it only take places after all the auto layout or auto resizing calculations on the views have been applied. Meaning the method viewDidLayoutSubviews is called every time the view size changes and the view layout has been recalculated.

Read More : AppCoda

Which method will first called if I came from back button?

Ans : 

The navigation controller sends viewWillAppear: to a view controller before putting its view on the screen, and viewDidAppear: after.

Inside viewWillAppear: and viewDidAppear:, the view controller can check self.isMovingToParentViewController.

If isMovingToParentViewController is YES, the view controller is being added to the navigation controller in the first place (presumably because it's the navigation controller's root view controller, or because it is being pushed).

If isMovingToParentViewController is NO, the view controller is already in the navigation controller's stack, and another view controller is being popped to reveal it.

Explain application states in iOS

Ans : 

There are 5 application states :

  1. Not Running
  2. Inactive
  3. Active
  4. Background
  5. Suspended.
iOS Application states
Application States


iOS Application Lifecycle : 

When an iOS app is launched the first thing called is

Initialise :

application: willFinishLaunchingWithOptions:-> Bool.
This method is intended for initial application setup. Storyboards have already been loaded at this point but state restoration hasn’t occurred yet.

Launch :

application: didFinishLaunchingWithOptions: -> Bool is called next. This callback method is called when the application has finished launching and restored state and can do final initialisation such as creating UI.

applicationWillEnterForeground: is called after application: didFinishLaunchingWithOptions: or if your app becomes active again after receiving a phone call or other system interruption.

applicationDidBecomeActive: is called after applicationWillEnterForeground: to finish up the transition to the foreground.

Termination :

applicationWillResignActive: is called when the app is about to become inactive (for example, when the phone receives a call or the user hits the Home button).

applicationDidEnterBackground: is called when your app enters a background state after becoming inactive. You have approximately five seconds to run any tasks you need to back things up in case the app gets terminated later or right after that.

applicationWillTerminate: is called when your app is about to be purged from memory. Call any final cleanups here.


Both application: willFinishLaunchingWithOptions: and application: didFinishLaunchingWithOptions: can potentially be launched with options identifying that the app was called to handle a push notification or url or something else. You need to return true if your app can handle the given activity or url.

Above all methods are defined in UIApplicationDelegate and AppDelegate.swift has to implement these methods.

If you have any comment, question, or recommendation, feel free to post them in the comment section below!

Difference between viewdidload and viewwillappear?

Ans : 


ViewDidLoad - It is excecuted once. So writer settings like set label text in ViewDidLoad.
ViewWillAppear - It is called every time when view appear.


ViewDidLoad - It is called when view is begin constructed.
ViewWillAppear - When view is about ready to appear.


ViewDidLoad It is automatically called when view controller completely loaded into memory. Override this method to perform additional initialization on views that were loaded from xib.
I.e instance variable initialization, database access, network request
ViewWillAppear It is called when View is about to added on view hierachy. If we want to change some, then we have to override this method.Like change orientation, change screen data.


Suppose your tableview data will be changed periodically. Then you have to write [tableview reloaddata] in ViewWillApper.

Read UIViewController LifeCycle

UIViewcontroller Lifecycle

Ans : A view controller manages set of views and making user interface. It will coordinate with data and other controller. Views are automatically loaded when view property is accessed in the app.
Following methods are used to mange view controller's view.

1. LoadView : It is automatically called when it's view property is accessed. It loads or create a view and assigned to property.

2. ViewDidLoad : It is automatically called when view controller completely loaded into memory. Override this method to perform additional initialization on views that were loaded from xib.
I.e instance variable initialization, database access, network request

Event Management to Views :

1. ViewWillAppear : It is called when View is about to added on view hierachy. If we want to change some, then we have to override this method.
Like change orientation, change screen data

2. ViewDidAppear : It is called when view was added on view's hierachy.
When we need to display loader, start UI animation ,then override this method.

3. ViewWillDisAppear : It is called when view is about to removed from hierachy. We can hide keyboard,  commit changes ,revert changes in this method by overriding.

4. ViewDidDisappear : It is called when view is removed from hierachy. We can remove cache data in this method.

Memmory Management method :
1. didReceiveMemoryWarning :
It is called automatically when system determine that the system has low amount of available memory.
Override this method remove not essential data from memory.


Ordering of excecuting methods :

1. Init(coder:)
2. (void)loadView
3. (void)viewDidLoad
4. (void)viewWillAppear
5. (void)viewDidAppear
6. (void)didReceiveMemoryWarning
7. (void)viewWillDisappear
8. (void)viewDidDisappear