简体   繁体   中英

Passing data between controllers ABC, from C to A

Consider three different controllers A, B and C where A is initial controller in Navigation Controller.

Now I push to B and then push to C.

A --> B --> C

Then I want to go from C to A. How can I go there with a single push and animation. I guess the way would be finding the controller A from the navigation stack and call popToController method. Let me know If any other better way.

Now main question is how can I pass the data from C --> A . If it's from B --> A I could use the delegation. But for C --> A what could be done to use the delegation?

I do have workaround by using observer, singleton, reactive patterns. But I would like to know if any better way can be used.

You can have multiple ways to achieve this functionality. Once can be get you VC from list and pop to it.

let viewControllers: [UIViewController] = self.navigationController!.viewControllers ;
    for aViewController in viewControllers {
        if let controllerA =  aViewController as? AViewController) {
           controllerA.data = self.data               
           self.navigationController?.popToViewController(controllerA, animated: true);
        }
    }

Also you might be interested in unwind segue . Here is a nice example too.

You can achieve this via this approach, it will work 100%

for item in self.navigationController?.viewControllers ?? [UIViewController]() {
        if item is FirstVC, let vc = item as? FirstVC {
            vc.data = ThirdVC.data //3rd vc data assign to 1st vc
            vc.callAnyMethodOfFirstVC()
            self.navigationController?.popToViewController(item, animated: true)
        }
    }

For passing data from C --> A use singleton pattern. any data you want to pass save in variable using singleton pattern and then use

 self.navigationController?.popToRootViewController(animated: true)

and now you can access data where you want. hope its help

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM