2017-09-03 19 views
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

+2

'let superView = BaseViewController()'不會工作,您需要*獲取* superview,而不是創建一個。 – luk2302

+0

那我該怎麼說呢? –

回答

0

此代碼永遠不會工作的權利,這是因爲:

  1. 您的新視圖控制器沒有加載,它不僅創造

  2. 您的新視圖控制器不父視圖控制器

檢查了這一點,瞭解如何獲得SuperViewController https://stackoverflow.com/a/24590678/5482826

相關問題