-1

I want to set the title, so I used: titleBar.topItem?.title = user!.name, where titleBar is a navigationBar.

@IBOutlet weak var titleBar: UINavigationBar!

but it keeps giving me fatal error; Here is my code: when I did print (user!.name), it will give me optional("why"), where "why" is the value of user!.name.

  var user:User! {
        didSet{
//        titleBar.topItem?.title = user!.name
        print("hhh")
        print("this is the name:",user!.name)
        print("hello")
        print("why???")
        }
    }

Even when I tried to use the user.name in a different function but in the same view controller file, it will still give me nil.

   print("sent to person: ", self.user?.name)

Can someone help me with this?

Katrina
  • 274
  • 1
  • 2
  • 13
  • `titleBar` is probably `nil` - At what point are you setting the `user` property? Does `titleBar` have a value at that point? – Paulw11 Jan 24 '19 at 02:45
  • when i set self.titleBar.topItem?.title = "hi", it gave me the same error. How can I set the value for titleBar? – Katrina Jan 24 '19 at 03:01
  • It isn't the value you are setting; it is the value you are trying to set - `titleBar` is an implicitly unwrapped optional - if you access it when it is `nil` you will get an exception. Where do you assign a value to this property - It is an `@IBOutlet`, so have you connected it in Interface Builder? At what point in the app execution are you trying to set it? Before `viewDidLoad` has run? – Paulw11 Jan 24 '19 at 03:04
  • If your view controller is embedded in a `UINavigationController` you would typically use `self.navigationController?.navigationBar` to get the `UINavigationBar` – Paulw11 Jan 24 '19 at 03:09

1 Answers1

0

You need to initialize titleBar before you can modify it's contents. Since this is a Storyboard reference, and viewDidLoad hasn't been hit yet. This @IBOutlet hasn't been initialized.

You'll want to try to transition away from the IBOutlet storyboard reference and use programatic AutoLayout (I added an example of what that might look like below), or try to initialize another variable within the didSet and assign it to titleBar. However on viewDidLoad that will be most likely overwritten.

// init titlebar
let titleBar: UINavigationBar = {
    var navigationBar = UINavigationBar()
    navigationBar.translatesAutoresizingMaskIntoConstraints = false
    return navigationBar
}()

After this you'll just need to add it to your view, and activate constraints via NSLayoutConstraint.activate(). Hope that helps.

KSigWyatt
  • 1,368
  • 1
  • 16
  • 33
  • that didnt solve the problem and didSet did run. When I ran public var user:User? { didSet{ print("\(user?.name)") // titleBar.topItem?.title = user?.name // self.titleBar.topItem?.title = "hi" print("\(user?.name)") print("hhh") print("this is the name:",user!.name) print("hello") print("why???") } } it printed out Optional("why") Optional("why") hhh this is the name: Optional("why") hello why??? – Katrina Jan 24 '19 at 03:02
  • Ah okay, I misread your question it looks like. I updated my answer. This is happening because you're trying to use `titleBar` before the ViewController has a chance to initialize it in `viewDidLoad`. I would recommend using programmatic AutoLayout for your UI element in this case. @Katrina – KSigWyatt Jan 24 '19 at 03:10