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      

Hacking with Swift is sponsored by Emerge

SPONSORED Why are Swift reference types bad for app startup time, and what’s the performance cost of protocol conformances? That’s just a couple of the topics you can learn about on the Emerge blog — written by the app performance experts behind Emerge’s advanced app optimization and monitoring tools, based on their experience of working at companies like Apple, Airbnb, Snap, and Spotify.

Find out more

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.