我收到此错误,无法解决...
我通过 UISegmentedControl 按钮更改将一个对象从 UIViewController 传递给另一个对象。下面是我用来传递对象的代码。它工作得很好,我可以在控制台中打印对象详细信息。
@IBAction func switchViewAction(_ sender: UISegmentedControl) {
let vc = DirectionsViewController()
if let unwrappedRecipe = self.details
{
vc.customInit(recipes: unwrappedRecipe)
} else
{
print("it has no value!")
}
self.viewContainer.bringSubviewToFront(views[sender.selectedSegmentIndex])
}
但是,问题是当我尝试为标签设置值时,出现以下错误:
Unexpectedly found nil while implicitly unwrapping an Optional value
下面是我在 DirectionsViewController 中使用的代码
@IBOutlet weak var lblDirections: UILabel!
var recipe: Recipe? = nil
override func viewDidLoad()
{
super.viewDidLoad()
// Do any additional setup after loading the view.
}
func customInit(recipes: Recipe)
{
lblDirections.text = recipes.name
}
我研究了可选和强制变量,也尝试安全地解开变量但没有运气。有人可以帮忙吗?