Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS viewDidLoad for UIView

In a ViewController, there is ViewDidLoad to know when the VC has loaded.

For a UIView, what method do i have to use when the view loaded?

Would this method be called with any init?

edit: No XIB, just programmatically.

like image 250
manuelBetancurt Avatar asked Apr 16 '12 01:04

manuelBetancurt


People also ask

What is Xcode viewDidLoad?

viewDidLoad is called when the ViewController has loaded its view hierarchy into memory. This is the point where you can perform your customized initialisation for your view controller. For instance, if your view controller has a UILabel and you want to set a custom text to it, this is the point where you do that.

What is the difference between viewDidLoad and viewWillAppear?

viewDidLoad: Whatever processing you have that needs to be done once. viewWilLAppear: Whatever processing that needs to change every time the page is loaded. Labels, icons, button titles or most dataInputedByDeveloper usually don't change.

What is a UIView iOS?

An object that manages the content for a rectangular area on the screen.

How many times is viewDidLoad called?

viewDidLoad() is one of the initialization methods that is called on the initial view controller. viewDidLoad() is called before anything is shown to the user - and it is called only once.


2 Answers

If you load it from a XIB file, the awakeFromNib method will be called once loading finishes:

override public func awakeFromNib() {     super.awakeFromNib();      // ... loading logic here ... } 

Update; In the case of no XIB, you will probably have to infer it using one of the methods from the Observing View-Related Changes area of the docs (for example, didMoveToSuperview). However, a better way is to send a message to your views from the view controller's viewDidLoad method if possible.

like image 126
borrrden Avatar answered Sep 19 '22 22:09

borrrden


You can use willMove(toSuperview newSuperview: UIView?)

import UIKit  final class myView: UIView {    override func willMove(toSuperview newSuperview: UIView?) {      super.willMove(toSuperview: newSuperview)      //Do stuff here    }  }  

Apple Docs

like image 39
davidrynn Avatar answered Sep 18 '22 22:09

davidrynn