-3

If we are implementing a page control such that we are adding small controllers on the scrollview. Although adding controller's view as a subview is not a good practice, but if this is the implementation, then, while adding the controller's view as a subview on the scrollview should the viewWillAppear method be invoked or not ? As per the current implementation, we are loading 4 pages initially. At this time, vieWillAppear does not invoked but when other pages are loaded while scrolling the scrollview, viewWillAppear gets invoked.

Please share your opinion.

iOSDevD
  • 30
  • 1

2 Answers2

0

View will appear :-This method is called before the receiver’s view is about to be added to a view hierarchy and before any animations are configured for showing the view. You can override this method to perform custom tasks associated with displaying the view. For example, you might use this method to change the orientation or style of the status bar to coordinate with the orientation or style of the view being presented. If you override this method, you must call super at some point in your implementation.

ViewDidLoad :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. You usually override this method to perform additional initialization on views that were loaded from nib files.

For more info visit: https://developer.apple.com/library/ios/documentation/uikit/reference/UIViewController_Class/Reference/Reference.html#//apple_ref/occ/instm/UIViewController/viewDidLoad

And one thing more, it would be great for you if you read documentation first.

Rana
  • 451
  • 4
  • 16
0

That is precisely the reason why adding a UIViewController's view as subview is a bad idea. Callback methods like viewWillAppear, viewDidAppear, viewWillDisappear and viewDidDisappear may or may not get called, very unpredictable. If you go down this road you will surely face issues. Read more on this from this elaborate answer.

You should use UIViewController containment in such scenarios. Check out the WWDC video available for this: Implementing UIViewController Containment. You may have to login to watch it (recommended to watch on Safari browser).

Also, have you checked PageControl sample code? May be it will help in your case. It uses view controller containment.

Hope that helps!

Community
  • 1
  • 1
Amar
  • 13,202
  • 7
  • 53
  • 71