我需要帮助了解使用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。
你确定你添加** ** purchaseSegue在故事板,而连接到PurchaseViewController?如果不是在创建关系时添加**“purchaseSegue”** ** ViewController **和** PurchaseViewController ** –
之间的关系**是......“SecondViewController”的segue具有“sendDataSegue”的标识符,并且segue为“ PurchaseViewController'有一个'purchaseSegueIdentifier'的标识 –