Reputation: 1045
I have a method running when a button is tapped:
let tempVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("AwardViewController") as! AwardViewController
tempVC.currentDocumentPartTitle = currentDocumentPart.title
And here is AwardViewController class,
class AwardViewController: UIViewController {
@IBOutlet weak var textView: UITextView!
var currentDocumentPartTitle: String!
override func awakeFromNib() {
super.awakeFromNib()
//assigning text to UITextView in awakeFromNib method
//In order to find right textfile, currentDocumentPartTitle is used to search right data in plist.
}
The problem is that even if I pass the variable, it returns nil value error in awakeFromNib method saying currentDocumentPartTitle
is nil. How do I pass a variable before awakeFromNib is called?
Upvotes: 0
Views: 799
Reputation: 72825
This happens because awakeFromNib()
is performed before the view's properties are allocatable. See https://stackoverflow.com/a/377390/1214800.
Why not simply do what you need to do inside of viewDidLoad()
?
override func viewDidLoad() {
super.viewDidLoad()
print(currentDocumentPartTitle!) // works!
}
Upvotes: 1
Reputation: 131398
You should put code that copies values of of properties and displays it into your UI in your destination view controller's viewWillAppear:animated:
method.
So for your AwardViewController
class, add code to your viewWillAppear:animated:
that installs the value of currentDocumentPartTitle
into your UI.
By the way, in the first part of your code you show creating the view controller, and setting the value of tempVC.currentDocumentPartTitle
, but you don't show any code to display the new view controller. Are you presenting it modally?
Upvotes: 0