1

I'm pulling out what little hair I have left!

I have a UIViewController with a matching .xib file. In the .xib, I have an IBOutlet that I made in interface builder called "titleText"

In a section of my code, I have the following lines:

let fooVC = FooViewController(nibName: "FooViewController", bundle: Bundle.main)
fooVC.titleText = "TEST"

The viewController "seems" to load fine, I get an object of the correct type, but the IBOutlets are not initialized.

Is there an additional step that I need to do to initialize any IBOutlets that I may have?

This has got to be something really simple, right?!

Cityzen26
  • 3,755
  • 3
  • 26
  • 30
Perlguy
  • 902
  • 9
  • 23

1 Answers1

1

Outlets are nil until the view loads. So just make another variable and store title string like below

class FooViewController: UIViewController {
    @IBOutlet titleText:UILable!
    var titleString:String?

    override func viewDidLoad() {
       super.viewDidLoad()
       titleText.text = titleString
    }
}

Use:

let fooVC = FooViewController()
fooVC.titleString = "TEST"
SPatel
  • 4,310
  • 4
  • 28
  • 50