点击按钮时我有一个方法运行:
let tempVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("AwardViewController") as! AwardViewController tempVC.currentDocumentPartTitle = currentDocumentPart.title这里是AwardViewController类,
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. }问题是,即使我传递变量,它在awakeFromNib方法中返回nil值错误,说currentDocumentPartTitle为nil。 如何在调用awakeFromNib之前传递变量?
I have a method running when a button is tapped:
let tempVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("AwardViewController") as! AwardViewController tempVC.currentDocumentPartTitle = currentDocumentPart.titleAnd 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?
最满意答案
发生这种情况是因为在视图的属性可分配之前执行了awakeFromNib() 。 请参阅https://stackoverflow.com/a/377390/1214800 。
为什么不在viewDidLoad()简单地做你需要做的事情?
override func viewDidLoad() { super.viewDidLoad() print(currentDocumentPartTitle!) // works! }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! }更多推荐
发布评论