0

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

  • 1
    You don't appear to be displaying `AddViewController` at all. Judging by your "dismiss" call, I'm guessing that `AddViewController` was the view displayed whatever your table view is in -- you need to store a reference to *that instance* of `AddViewController` and not make a new one. Also, unrelated to the issue, but your `let viewController: UIViewController = UIViewController()` looks like it might not do anything. – jnpdx Jan 14 '22 at 01:00

2 Answers2

1

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 :

func changeValue(myValue : Bool) {
    if let navController = SecondViewController as? UINavigationController {
       let getfirstVC = navController.topViewController as! FirstViewController
        getfirstVC.chosenDaily = myValue
    }
}

and your tableview function will be like this :

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        let viewController: UIViewController = UIViewController()

        switch (indexPath.row) {
               case 0:
                   print("hello")
                changeValue(true)
                dismiss(animated: true, completion: nil)

Don't forget to change FirstViewController and SecondViewController according to your own.

  • Thanks for the answer but I believe the function ChangeValue() does not conform to Swift's naming convention. – Lawrence Gimenez Jan 14 '22 at 11:10
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jan 14 '22 at 11:11
0

You can use Delegate to interactive between view.

protocol tableViewControllerDelegate {
    func change(value:Bool)
}

class tableViewController: UIViewController {
    var del:tableViewControllerDelegate!

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        switch (indexPath.row) {
               case 0:
                del?.change(value:true)
                dismiss(animated: true, completion: nil) 
                break;
        default:
            break
        }
    }
}

Class is reference type, You can also processing data by class.

class AddViewController:UIViewController,tableViewControllerDelegate{
    var chosenDaily = false

    func change(value:Bool){
        chosenDaily = value
    }
}




class tableViewController: UIViewController {
    var data:Sample!

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        switch (indexPath.row) {
               case 0:
                data?.chosenDaily = true
                dismiss(animated: true, completion: nil)
                break;
        default:
            break
        }
    }
}

class Sample{
    var chosenDaily = false
}

class AddViewController:UIViewController{

    var data = Sample()

    func open(){
        let view = tableViewController()
        view.data = data
    }
}
Leo Chen
  • 327
  • 1
  • 8