我需要帮助理解使用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语句的第一个分支(如果购买了)可以很好地工作,但是第二个分支(如果没有购买)会给出一个错误:"unexpectedly nil while unwrapping Optional value“。
destination!.medName
显示为nil
,但仅在第二个分支中显示。这是我不明白的地方。
这是a screenshot of my storyboard。
发布于 2016-09-23 03:59:25
让destination
成为nil
的唯一方法就是我知道造型不起作用。所以我认为segue.destinationViewController
不是PurchaseViewController
的一个实例
若要仔细检查打开情节提要,请选择视图控制器,然后打开属性检查器。检查那里的类名。
https://stackoverflow.com/questions/39647159
复制相似问题