0
我有一個名爲'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!)")
}
當我點擊的單人或兩個玩家(或當函數被調用)它給了我一個錯誤說致命錯誤:意外發現零而展開的可選值
THX
'let superView = BaseViewController()'不會工作,您需要*獲取* superview,而不是創建一個。 – luk2302
那我該怎麼說呢? –