简体   繁体   English

从MasterViewController在DetailViewController中设置detailItem时出现奇怪的错误

[英]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.

相关问题 弹出MasterViewcontroller时推送DetailViewController - push DetailViewController when popping MasterViewcontroller 无法从MasterViewController访问DetailViewController - Unable to access DetailViewController from MasterViewController 在SplitViewController中从DetailViewController隐藏MasterViewController - Hide MasterViewController from DetailViewController in a SplitViewController 如何在Swift中获取detailViewController以在其masterViewController.tableView中显示上一行或下一行的detailItem? - How to get the detailViewController to display the detailItem of the previous or next row in its masterViewController.tableView in Swift? 在UISplitViewController应用程序中从DetailViewController获取MasterViewController - getting the MasterViewController from the DetailViewController in a UISplitViewController app 将数据从detailViewController向后传递到masterViewController - Pass data backward from detailViewController to masterViewController 使用prepareForSegue显示从MasterViewController到DetailViewController的字符串 - Displaying a string from MasterViewController to DetailViewController using prepareForSegue MasterViewController更新时如何刷新DetailViewController - How to refresh DetailViewController when MasterViewController is updated 我应该如何从更改的masterViewController跟踪detailViewController? - how should I keep track of a detailViewController from a changing masterViewController? ISplitViewController:DetailViewController可以从MasterVIewController加载数据而不需要回传吗? - ISplitViewController: Can DetailViewController load data from MasterVIewController without segueing back?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM