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
InfoViewController
from the storyboard, not by creating a new instance... – Hillel