2016-09-22 52 views
0

我需要帮助了解使用segues的导航,并且无法在此处或在Apple文档中找到合适的答案。从一个视图控制器传递相同数据的两个段落

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

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

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

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

我正在使用NSUserDefaults存储一本关于是否已购买冥想的信息的字典。

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

如果购买我想'SecondViewController'来显示,我想传递它两个变量。 如果没有购买,我想'PurchaseViewController'显示,我也需要传递相同的两个变量。

这里是我使用的代码:

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语句(如购买)的第一支作品完美,但第二个(如果不购买)给出了一个错误“意外发现零而展开的可选值“。

destination!.medName显示为nil,但仅在第二个分支中显示。这是我无法理解的部分。

这里是a screenshot of my storyboard

+0

你确定你添加** ** purchaseSegue在故事板,而连接到PurchaseViewController?如果不是在创建关系时添加**“purchaseSegue”** ** ViewController **和** PurchaseViewController ** –

+0

之间的关系**是......“SecondViewController”的segue具有“sendDataSegue”的标识符,并且segue为“ PurchaseViewController'有一个'purchaseSegueIdentifier'的标识 –

回答

0

destinationnil的唯一方法就是我不能使用该投射。所以我认为segue.destinationViewController不是PurchaseViewController的实例

要仔细打开故事板,请选择您的视图控制器并打开属性检查器。在那里检查课程的名称。

enter image description here

+0

我不是100%确定如何检查,但... 在'PurchaseViewcontroller'的Storyboard上显示的是'PRESENTING SEGUES'部分中的关系。 show - > ViewController手册 –

+0

我编辑了我的答案,更多细节。 – IgnazioC

+0

感谢您的澄清。 。 我的'SecondViewController'具有'SecondViewController'作为类, 'ViewController'具有'ViewController'作为类 ,'PurchaseViewController'具有'PurchaseViewController'作为类 –

相关问题