首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >从一个视图控制器传递相同数据的两个segues

从一个视图控制器传递相同数据的两个segues
EN

Stack Overflow用户
提问于 2016-09-23 03:12:58
回答 1查看 69关注 0票数 0

我需要帮助理解使用segues的导航,但在这里或在Apple文档中找不到合适的答案。

我正在构建一个冥想音频播放器。

我有一个主要的'ViewController‘,它显示了不同冥想的表格。

我有一个'SecondViewController‘播放’冥想‘音频文件。

我有一些免费的和一些付费的。

我正在使用NSUserDefaults来存储一个字典,里面有关于冥想是否已经购买的信息。

如果用户单击主'ViewController‘中的表单元格,它应该检查它是否已被购买。

如果购买了,我想要显示'SecondViewController‘,并且我想传递两个变量。如果没有购买,我希望'PurchaseViewController‘显示,我也需要传递相同的两个变量。

下面是我使用的代码:

代码语言:javascript
运行
复制
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

    var index = tableView.indexPathForSelectedRow?.row

    if pDictionary[purchasedArray[index!]]=="Purchased"||pDictionary[purchasedArray[index!]]=="Included"{
        let medSegueIdentifier = "sendDataSegue"
            if segue.identifier == medSegueIdentifier {
                index = tableView.indexPathForSelectedRow?.row}
        let destination = segue.destinationViewController as? SecondViewController
        destination!.medName = names[index!]
        destination!.purchased = true

    } else if pDictionary[purchasedArray[index!]]=="NotPurchased"{
        let medSegueIdentifier = "purchaseSegueIdentifier"
            if segue.identifier == medSegueIdentifier {                  
                index = tableView.indexPathForSelectedRow?.row}
        let destination = segue.destinationViewController as? PurchaseViewController
        destination!.medName = names[index!]
        destination!.purchased = false
    }

if语句的第一个分支(如果购买了)可以很好地工作,但是第二个分支(如果没有购买)会给出一个错误:"unexpectedly nil while unwrapping Optional value“。

destination!.medName显示为nil,但仅在第二个分支中显示。这是我不明白的地方。

这是a screenshot of my storyboard

EN

回答 1

Stack Overflow用户

发布于 2016-09-23 03:59:25

destination成为nil的唯一方法就是我知道造型不起作用。所以我认为segue.destinationViewController不是PurchaseViewController的一个实例

若要仔细检查打开情节提要,请选择视图控制器,然后打开属性检查器。检查那里的类名。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39647159

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档