【发布时间】:2017-09-03 10:49:14
【问题描述】:
我在一个名为“BaseViewController.swift”的文件中有一个函数。当我在 viewDidLoad(在同一个文件中)中调用它时,它可以完美运行。
func setFrontView(to view: UIView) {
if view == singlePlayerContainerView {
singlePlayerContainerView.isHidden = false
twoPlayerContainerView.isHidden = true
} else if view == twoPlayerContainerView {
singlePlayerContainerView.isHidden = true
twoPlayerContainerView.isHidden = false
} else {
/
singlePlayerContainerView.isHidden = false
twoPlayerContainerView.isHidden = true
}
}
我尝试在另一个名为“PlacesView.swift”的文件中调用它,在 collectionView DidSelectItem 中:
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let cell: PlacesCollectionViewCell = collectionView.cellForItem(at: indexPath) as! PlacesCollectionViewCell
let superView = BaseViewController()
switch cell.modeTitleLabel.text! {
case "Single Player":
superView.setFrontView(to: superView.singlePlayerContainerView)
print("incomplete func")
case "Two Player":
superView.setFrontView(to: superView.twoPlayerContainerView)
print("incomplete func")
case "Lie Mode":
print("incomplete func")
case "Master's Mode":
print("incomplete func")
case "Settings":
print("incomplete func")
default:
print("incomplete func")
}
print("Cell was tapped! Title: \(cell.modeTitleLabel.text!)")
}
当我点击单人游戏或双人游戏(或调用函数的位置)时,它给我一个错误,说致命错误:在展开可选值时意外发现 nil
谢谢
【问题讨论】:
-
let superView = BaseViewController()不起作用,您需要获取超级视图,而不是创建超级视图。 -
那我怎么说呢?