UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

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.

3      

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.

3      

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.

3      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

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.