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?!

Rumin
  • 3,787
  • 3
  • 27
  • 30
Perlguy
  • 982
  • 10
  • 25

1 Answers1

2

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,768
  • 4
  • 32
  • 51