[英]Weird Error when setting detailItem in DetailViewController from MasterViewController
I started getting this error randomly (may be due to updating swift) but when I reach, didSet in detailItem, I call configureView. 我开始随机收到此错误(可能是由于更新速度很快),但是当我到达detailItem中的didSet时,我调用了configureView。 In configureView I check to see if detailItem is indeed set and then start assigning values to my outlets.
在configureView中,我检查是否确实设置了detailItem,然后开始为我的插座分配值。
if let detail: Posting = self.detailItem {
print("detail title is ")
println(detail.title)
// Title
self.titleLabel.text = detail.title
However this crashes with output: 但是,这崩溃与输出:
detail title is Skiing in Vail
fatal error: unexpectedly found nil while unwrapping an Optional value
The error is on the line: 错误就在网上:
self.titleLabel.text = detail.title
I don't understand why it is crashing when it is is clearly set... Note that this doesn't happen if I call configureView
from within viewDidLoad. 我不明白为什么它在明确设置后会崩溃...请注意,如果我从viewDidLoad中调用
configureView
,则不会发生这种情况。
This only happens when I call it from 只有当我从
var detailItem: Posting? {
didSet { self.configureView() }
}
Something I'm missing? 我缺少什么? Is this working asynchronously or something?
这是异步工作还是什么?
Referencing your comment on how to implement this, there are a few trains of thought. 参考您对如何实现此目的的评论,有一些思路。 First, the reason you can modify it directly from your MasterVC is because the IBOutlet hasn't been instantiated in the DetailVC and thus isn't available to be modified yet.
首先,您可以直接从MasterVC对其进行修改的原因是,尚未在DetailVC中实例化IBOutlet,因此尚无法进行修改。
One option, which I'd probably follow, is to have a helper variable on your detail view where you can place the value. 我可能会遵循的一个选择是,在详细信息视图上有一个辅助变量,您可以在其中放置值。 Then
viewWillAppear()
you can take whatever's in the variable and set it as the label's text. 然后,
viewWillAppear()
可以获取变量中的所有内容并将其设置为标签的文本。 Here's a tutorial on it, but to answer your specific quesiton please jump to the prepareForSegue
method in this tutorial. 这是一个教程,但是要回答您的特定问题,请跳至本教程中的
prepareForSegue
方法。 It gives a good rundown of the whole process: 它提供了整个过程的良好总结:
http://www.codingexplorer.com/segue-swift-view-controllers/ http://www.codingexplorer.com/segue-swift-view-controllers/
Let me know if this doesn't solve your problem. 让我知道这是否不能解决您的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.