I'm building an app (in XCode 8.2.1) where some objects are displayed on a 2D board, and when the user taps one of these objects some info should be displayed about it as a styled modal info box. My design is to have the info written in a separate view controller, which I would display when needed.
I've designed a basic stub for the second view controller and added a single label to it in the interface builder. Then I've ctrl-linked this label to my custom VC class:
class InfoViewController: UIViewController {
@IBOutlet weak var info: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
}
func displayInfo() {
info.attributedText = NSAttributedString(string: "abc")
}
}
However, when I test my app and tap the object, the info
field is nil
even in the viewDidLoad()
method of my custom VC class. The way I'm displaying my VC is as follows:
let infoViewController = InfoViewController()
infoViewController.modalPresentationStyle = .overCurrentContext
self.present(infoViewController, animated: true, completion: nil)
infoViewController.displayInfo()
(Note: In the end I will have only one single instance of InfoViewController
but this is just for testing. I don't expect having a global instance would make any difference?)
As I said, be it inside the viewDidLoad()
method or in the displayInfo()
method, info
is always nil
, such that setting its attributedString
attribute crashes the app. Thinking the present
method might be called asynchronously, I've tried calling displayInfo()
from inside viewDidLoad()
, but that didn't make any difference.
Can anyone tell my what I've forgotten that would allow my IBOutlet
from being properly initialized properly?
Thanks!
David
The problem is the reference to
InfoViewController()
, which instantiates the view controller independent of any storyboard scene. You want to useinstantiateViewController
:A couple of notes:
This assumes that (a) you've given the scene in the storyboard a "storyboard id"; (b) you've set the base class for that scene to
InfoViewController
.Note, I called
displayInfo
in the completion handler ofpresent
because you probably don't want that called until the scene has been presented and the outlets have been hooked up.Alternatively, you can update non-outlet properties of the
InfoViewController
immediately after instantiating it and then have itsviewDidLoad
take those properties and update the outlets, e.g.:Note, I changed the
@IBOutlet
name to beinfoLabel
and added theString
property calledinfo
. That tends to be the convention, that outlets bear some suffix indicating the type of control, and model objects, like theString
property, are without the suffix. (You'll just want to make sure you remove that old outlet in the connections inspector in IB so that you don't have problems with these property name changes.)Anyway, you can then do:
The key point is don't try to update outlets of the scene immediately after instantiating it, but make sure that this is deferred until after
viewDidLoad
was called.I Replaced
With
Finally
Now It Works...
In my case, I had created new view controller for the same class. Which had ended up with two view controllers in storyboard, but referring to the same class. After deleting old view controller, everything worked fine.
Hope it helps to someone.