Passing Data Between ViewControllers

How do we pass data from one controller to another, there are many posts on this but this is the easiest way

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        
        // get a reference to the second view controller
        let secondViewController = segue.destination as! SecondViewController
        //secondViewController.receivedData
        // set a variable in the second view controller with the data to pass
        secondViewController.receivedBusinessData = business
    }    // method to run when table view cell is tapped

In the second view controller just add the variable, and you can access the data

var receivedBusinessData: Business? = nil

    override func viewDidLoad() {
        super.viewDidLoad()
        print("yy zz", receivedBusinessData?.name!)
        print("yy zz", receivedBusinessData?.address!)
        print("yy zz", receivedBusinessData?.id!)
        if(receivedBusinessData?.photos != nil ){
            for i in 0..<receivedBusinessData!.photos!.images.count{
                print("photos image url yy zz", receivedBusinessData!.photos!.images[i])
            }
        }        // Do any additional setup after loading the view.
    }

declare the variable in the second view, and you can access the data.

Leave a comment

Your email address will not be published. Required fields are marked *