0

I'm trying to instantiate a storyboard from a button press inside a collection view cell but am coming across errors:

class sampleCell: UICollectionViewCell {

    @IBAction func infoButtonPressed(_ sender: Any) {

        let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "InputViewController") as! InputViewController//error: Use of undeclared type "InputViewController"

        present(vc, animated: false)//error:Use of unresolved identifier 'present'
    }
}

Any idea how to do this?

SwiftyJD
  • 5,257
  • 7
  • 41
  • 92

2 Answers2

0

1- You don't have a class named InputViewController

let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "InputViewController") as! InputViewController 

2- You can't present a vc inside a cell , you need a delegate

present(vc, animated: false) 

A note only for main storyboard you can substitute

UIStoryboard(name: "Main", bundle: nil)

with ( But inside a vc also )

self.storyboard

Edit: ---------------------------------------------------------

inside the cell

class sampleCell: UICollectionViewCell {
  weak var delegate:CurrentController?
}

Then inside cellForItemAt

let cell = /////
cell.delegate = self

Add to the vc

func goToInput() {

    let vc = self.storyboard!.instantiateViewController(withIdentifier: "InputViewController") as! InputViewController//error: Use of undeclared type "InputViewController" 
    present(vc, animated: false) 

}

After that inside cell action

@IBAction func infoButtonPressed(_ sender: Any) {
  delegate?.goToInput()
}
Shehata Gamal
  • 98,760
  • 8
  • 65
  • 87
0

You are facing the error because the present(_:animated:completion:) is a UIViewController instance method, but not a UITableViewCell.

So, what you should instead is to figure out a way to handle the button action in the view controller that contains the table view instead of trying to present a view controller from the cell class.

Considering that you would follow the following answer: https://stackoverflow.com/a/28895402/5501940

you would be able to call the present method in the button selector method:

@objc func buttonTapped(sender: UIButton) {
    let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "InputViewController") as! InputViewController
    present(vc, animated: false)
}

Also, make sure that you have a InputViewController view controller.

Ahmad F
  • 30,560
  • 17
  • 97
  • 143