Home > Software design >  Calling a method from another view controller
Calling a method from another view controller

Time:08-12

I am trying to call a method from one view controller into another view controller, but it's not working. I also tried looking into similar StackOverflow problems but I wasn't able to get an answer.

Method from the First View Controller that I want to call (class is called SoprtsController)

Second View Controller where I want to call the method (class is called LastViewController

CodePudding user response:

pass data between two view controllers you could check these tutorials

https://www.hackingwithswift.com/example-code/system/how-to-pass-data-between-two-view-controllers

CodePudding user response:

Like someone else mentioned it's usually not good practice to call another view controller's functions.

Make sure your variable test has a value before you try to use it.

Inside of viewDidLoad you can set a value test = SportsController() Or set a value when creating whatever view controller you're in. lastVC.test = SportsController()

Next time post your code in the question! It makes it easier to see the problem.

func getCurrentSport() -> sports? {
   return currentSport
}
var selectedSport: sports?
var test: SportsController?
var testSport: sports?

override func viewDidLoad() {
   super.viewDidLoad()
   testSport = test?.getCurrentSport()
   print(testSport?.sportName)
}
  • Related