I am trying to get data from a different screen but for some reason, the variable does not update. here is the code so far
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let viewController: UIViewController = UIViewController()
switch (indexPath.row) {
case 0:
print("hello")
let vc = AddViewController()
vc.chosenDaily = true
dismiss(animated: true, completion: nil)
here I created a tableView and once the user clicks on the first index of my tableView I want to set a boolean variable to true and return to another screen. I declared this boolean variable on the other screen here.
var chosenDaily:Bool = false
on the same screen, I have a save button and want to print the result.
@IBAction func didTapSaveButton() {
if chosenDaily == true {
print("it's true")
} else if chosenDaily == false {
print("it's false")
}
}
but for some reason, the code doesn’t update and returns false instead of true. can anyone help? thanks for any help that i get
2
Answers
You can use Delegate to interactive between view.
Class is reference type, You can also processing data by class.
I don’t know if you are using navigation controller during the transition between screens. But I suppose you are using it. So let’s say you have two ViewController. FirstViewController and SecondViewController. And they are also using navigation controller when you make a transition from FirstViewController to the SecondViewController. In the SecondViewController you have your tableView and when you click a row in that tableView you want to send some value to the FirstViewController before dismiss. Add this function in the SecondViewController and call it before dismiss :
and your tableview function will be like this :
Don’t forget to change FirstViewController and SecondViewController according to your own.