Best Moments
Best Moments

Reputation: 141

iOS How to call func from another View Controller Swift

I have one problem, i want call one method from firstVC to seccondVC i try to use such code:

let storyboard: UIStoryboard = UIStoryboard.init(name: "MainScreenVC", bundle: nil)
let firstViewController: MainScreenVC = storyboard.instantiateViewController(withIdentifier: "MainScreenVC") as! MainScreenVC
firstViewController.automatingFullFill()

But immediately i received error, what is wrong?

My error:

'Could not find a storyboard named 'MainScreenVC' in bundle NSBundle

Upvotes: 1

Views: 540

Answers (1)

Malleswari
Malleswari

Reputation: 427

If you are taking seperate storyboard for every viewcontroller you have to give storyboard name and viewcontroller name .

let storyboard: UIStoryboard = UIStoryboard.init(name: "give storyboard name here ", bundle: nil)
let firstViewController: MainScreenVC = storyboard.instantiateViewController(withIdentifier: "viewcontrollername here") as! MainScreenVC
firstViewController.automatingFullFill()

(OR) You can call it like this also

 let mainScreenVC = UIStoryboard.init(name: "give Storyboard name here", bundle: nil).instantiateViewController(withIdentifier: "viewcontroller") as! MainScreenVC

Upvotes: 2

Related Questions