override func viewDidLoad() {
super.viewDidLoad()
print(" viewDidLoad")
// Do any additional setup after loading the view, typically from a nib.
}
there is any problem if super.viewDidLoad() from above function
override func viewDidLoad() {
super.viewDidLoad()
print(" viewDidLoad")
// Do any additional setup after loading the view, typically from a nib.
}
there is any problem if super.viewDidLoad() from above function
Yes, it takes away the opportunity of the super
to do anything in viewDidLoad()
.
If the super
doesn't do anything (which may be the case if you are subclassing UIViewController
directly), then you may not see any immediate difference. But if you were to subclass the class you just wrote and not call the super
, for example, then your print would not be called.
See more: Do I always have to call [super viewDidLoad] in the -viewDidLoad method?
For a VC inheriting from UIViewController
, not calling super.viewDidLoad
makes no difference.
However, when you have a VC that inherits from a class that actually does something in its viewDidLoad
will make a difference.
I once made a board game and I had a GameViewController
that shows the game screen for a two player game. GameViewController.viewDidLoad
does some setup of the game board view and the game model and so on. Then one day I decided to write an AI for the game so that people can play even when they are alone. The game with the AI is a little different from a two player game, so I made a AIGameViewController
, which inherits GameViewController
.
In AIGameViewController
, I must call super.viewDidLoad
, or else I have to copy and paste the code in GameViewController.viewDidLoad
.
There is a library called "Eureka" that allows you to create forms for users to fill in. To use it, you create a new VC that inherits from FormViewController
, and then you can start to add rows to the form in viewDidLoad
. It is required to call super.viewDidLoad
here because FormViewController
will, among other things, set up a table view to display the form. Without the table view, your form will not be displayed.