I’ve been studying swift for 2 days, and I have a problem:(
I have two ViewControllers and I need to call a func in the first from the second.
E.g. I want to add one to variable c in the first ViewController from the second:
class FirstViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
var c = 0
func c_count() {
c += 1
}
}
class SecondViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func button(_sender: Any) {
//call c_count()
}
}
I tried to use:
@IBAction func button(_sender: Any) {
let vc = FirstViewController()
vc.c_count()
}
but it said ‘Thread 1: breakpoint 1.1 (1)’
2
Answers
Welcome to SO, and to Swift!
Your approach can’t work because a fresh instance won’t have the real data. Every time you instantiate a new
FirstViewController
,c
will be set to 0. The instance of FVC that you want is the one that’s in the view hierarchy, so even if you didn’t get that breakpoint message,c
would not be updated in the UI.There are many ways to overcome this problem, and the one that works best will depend on the specific needs of your program. Here are a few possibilities:
IBAction
message, even if it’s triggered from a child view.FirstViewController
instance to a variable whereSecondViewController
can reach it. For example, you could save a weak pointer toFVC
into a delegate variable onSVC
and formalize the relationship using a protocol.Given you’re new, I’d guess you want either 1 or 2. The last two can be fragile and difficult to debug, so should only be attempted if a simpler approach isn’t possible.
you can create a DataSource class that adopts the necessary protocol. Here’s a basic example:
In each of your UIViewControllers, create an instance of MyDataSource