D
D
D_vers2017-08-07 12:20:52
Swift
D_vers, 2017-08-07 12:20:52

How to use the functions of other controllers?

I have a VC1, it has a function that uses a function from VC2, but when I call func1 from VC3, nothing works
class VC1: UIViewController {
controllerVC2: VC2?
func func1() {
controllerVC2 = VC2()
controllerVC2.controllerVC1 = self
controller.printOk()
}
}
class VC2: UIViewController {
controllerVC1: VC1?
func printOk() {
print("ok")
}
}
class VC3: UIViewController {
controllerVC1: VC1?
func func() {
controllerVC1 = VC1()
controllerVC1.controllerVC1 = self
controllerVC1.func1()
}
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander Aksentiev, 2017-08-07
@Sanasol

Write methods in models, not in controllers.
Controllers are not for that.

I
iMaximus, 2017-08-07
@iMaximus

class ViewController1: UIViewController {
class func func1() {
ViewController2.printOk()
}
}
class ViewController2: UIViewController {
class func printOk()
{
print("ok")
}
}
class ViewController3: UIViewController {
override func viewDidLoad() {
super. viewDidLoad()
ViewController1.func1()
}
}
Everything works, I checked it. Only this vseravno game. A ready-made example of how not to do it. And what you have, I don’t even want to comment :)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question