Home > Back-end >  Accessing data from different screens - swift
Accessing data from different screens - swift

Time:01-15

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

CodePudding user response:

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.

CodePudding user response:

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
    }
}
  •  Tags:  
  • Related