0

I have a function in a file called 'BaseViewController.swift'. And it works perfectly when I call it in viewDidLoad (in the same file).

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  
    }  
}  

I tried calling it in another file called 'PlacesView.swift', in 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!)")  

}  

When I tap on Single Player or Two Player (or where the function is called), it gives me an error saying fatal error: unexpectedly found nil while unwrapping an Optional value

Thx

1 Answers1

0

This code will never work right, because:

  1. Your new ViewController didn't load, it's only created

  2. Your new ViewController isn't parent ViewController

Check this out to find out how to get SuperViewController https://stackoverflow.com/a/24590678/5482826

Yerken
  • 299
  • 1
  • 12