WWDC22 SALE: Save 50% on all my Swift books and bundles! >>

The right way for implementation of a view hierarchy in UIViewController

Forums > iOS

Hello to everybody 👋

I and my coworkers can't solve a dispute about initialization of a view hierarchy for a long time. We implement a view hierarchy from code, without using Interface Builder. Each of us has own background experience about how it should work "right". If everything is clearly and understandably in Objective-C, there are some questions in Swift on which Apple hasn't answered.

We have three technical solutions and each of us applies own solution:

1️⃣

class SomeClass: UIViewController {

    private var someLabel: UILabel!
    private var someSwitch: UISwitch!

    override func viewDidLoad() {
        super.viewDidLoad()

        let someLabel = UILabel()
        someLabel.text = "Test"
        view.addSubview(someLabel)
        self.someLabel = someLabel

        let someSwitch = UISwitch()
        someSwitch.isOn = true
        view.addSubview(someSwitch)
        self.someSwitch = someSwitch
    }
}

2️⃣

class SomeClass: UIViewController {

    private let someLabel = UILabel()
    private let someSwitch = UISwitch()

    override func viewDidLoad() {
        super.viewDidLoad()

        someLabel.text = "Test"
        view.addSubview(someLabel)

        someSwitch.isOn = true
        view.addSubview(someSwitch)
    }
}

3️⃣

class SomeClass: UIViewController {

    private lazy var someLabel = UILabel()
    private lazy var someSwitch = UISwitch()

    override func viewDidLoad() {
        super.viewDidLoad()

        someLabel.text = "Test"
        view.addSubview(someLabel)

        someSwitch.isOn = true
        view.addSubview(someSwitch)
    }
}

I suppose that the same question has already discussed somewhere, but I haven't found a discussion here. If you know where there was the discussion, please, send to me.

1      

To me these approaches dont seem ideal because they dont scale. What if need to create and configure 10 UI controls?

If you are creating UI in code then you should use the loadView override and not viewDidLoad.

I would also consider creating custom subclasses of UIView to avoid having all the view code in view controller.

1      

To me these approaches dont seem ideal because they dont scale. What if need to create and configure 10 UI controls?

Oh, It was just the examples 🙂 Of course I'd implement like this:

class SomeClass: UIViewController {

    private var customView: CustomView!
    ...
}

class CustomView: UIView {

    private let someLabel = UILabel()
    private let someSwitch = UISwitch()
    ...
}

If you are creating UI in code then you should use the loadView override and not viewDidLoad.

But why? According to Apple documentaion I can override either loadView() or viewDidLoad.

1      

Save 50% in my Black Friday sale.

SAVE 50% To celebrate WWDC22, all our books and bundles are half price, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.

Save 50% on all our books and bundles!

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.