Search Your Question

Showing posts with label ViewController. Show all posts
Showing posts with label ViewController. 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.

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

Collectionview or Scrollview or PageViewController, Which one is most preferable?

It depends on requirement. If too many pages or items, then Collectionview or pageviewcontroller is good option. Because Collectionview can reuse cell for display view, whereas PageViewController load only current page,previous page and next page in memory. As Scrollview loads all subviews in memory, it will consume too much memory.

Pageviewcontroller load multiple viewcontroller in memory. So preferable most in following order :

1. UICollectionview
2. UIPageViewController
3. UIScrollView


UITableview and UICollectionview

Similarity :
The way to setup both with cell registration, dequeing cells, specifying size and heights are pretty much the same.

Diffrence :
1. The collection view is much more robust in terms of layout features and other things like animations.
Tableiw is a simple list, which displays single-dimensional rows of data. It’s smooth because of cell reuse and other magic.

2. UICollectionView is the model for displaying multidimensional data . 
UITableViewhas a couple of styles of cells stacked one over the other. You should not try to bend it to do any other kind of things too much complex than that kind of layout.

3. UICollectionView is a much more powerful set of classes that allow modifying almost every aspect of how your data will appear on the screen, especially its layout but also other things.

Advantage of Collectionview :
The biggest advantage of using collection view is the ability to scroll horizontally.
 If you want multiple columns in your apps then UICollectionView is the best way to do it. 


It is possible to have multiple columns in table view as well but it gets really messy since you are dealing with and array to display data in a table view.

UICollectionView Supports complex layouts. Apple provides you with something called UICollectionViewDelegateFlowLayout which provides you the flow of left and right as well as up and down.

UICollectionView supports custom animations based on different layouts which again cannot be done in UITableView.

Disadvantage of Collectionview (Advantage of Tablvieview over) :

Major disadvantages of using UICollectionView is auto sizing of your cells. It takes a lot of trial and error to get auto sizing to work correctly. UITableView wins here as all you have to do is return UITableView automatic dimensions for the height of your row in each one of your cells.

Summary :
If you need more control over your layout, go with UICollectionView, If you need simple list go with UITableview.