How I can transfer data between controllers when returning to the last screen? Swift

Related searches

In the application, I am asked to select a category, when I click, I move to the next screen from the tableView, and when I click on the desired category, it should be saved and return to the previous controller, where instead of "select a category" my chosen category will be.

I using by this method navigationController?.popViewController(animated: true), which brings me back to the last screen. As I understand it, prepare is not called, but since I know exactly where I'm going, I can, in the method from which I call pop ..., get access to the controller I am switching to and pass it the necessary properties? But how?

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  let currentCell = categories[indexPath.row]
    /?
    /?
  navigationController?.popViewController(animated: true)
}

When you go to the select screen do

let vc = ///
vc.delegate = self
// push

then inside didASelectRowAt

class CategoryViewController:UIViewController {
 weak var delegate:PreviousVC?
 ////
}

delegate?.sendData(someData)
navigationController?.popViewController(animated: true)

Another option inside didASelectRowAt to

// access previous vc
let previousVC = self.navigationController!.viewControllers[self.navigationController!.viewControllers.count - 2] as! PreviousVc
previousVC.sendData(someData)
// pop here

Edit: inside the firstVC

     override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if let secondVC = segue.destination as? SecondViewController {
          secondVC.delegate = self   
        }
    }

How I can transfer data between controllers when returning to the , Here are all the ways in which you can pass data between view controllers: when a segue is performed. triggering transitions programmatically in your code. through the state of the app. using singletons. using the app delegate. assigning a delegate to a view controller. through unwind segues. If you have a value in one view controller and want to pass it to another, there are two approaches: for passing data forward you should communicate using properties, and for passing data backwards you can either use a delegate or a block. Passing data forward is used when you want to show some information in a detail view controller.

You can pass the selected category to the previous view controller using closure

class FirstViewController: UIViewController {   
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if let secondVC = segue.destination as? SecondViewController {
            secondVC.selectedOption = { selectedOption in
                print(selectedOption)
                //change category title here
            }
        }
    }

}
class SecondViewController: UIViewController {
    var selectedOption: ((String) -> Void)?
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        selectedOption?("Selected Option String")
        navigationController?.popViewController(animated: true)
    }
}

Passing Data Between View Controllers in iOS: The Definitive Guide, If you have a value in one view controller and want to pass it to another, there are two approaches: for passing data forward you should´┐Ż Passing data between view controllers is a very common task. Depending on the circumstances, there are several ways to accomplish this. Hint: This post is using Swift 3, iOS 10 and Xcode 8.2.1. Generally speaking, there are two cases: Passing data to the next view controller, or passing data to the previous view controller.

Maybe you should pass "Completion handler" when you move to your category selection controller like

(String)->(Void)

and when you pop it, pass your category in this clouser

How to pass data between two view controllers, Then select the segue between view controller, and set an identifier for You can pass the data to the next view controller here. So now you want to pass a data to the previous view controller (InputViewController) as the current view DisplayViewController.swift protocol DisplayViewControllerDelegate´┐Ż Question about passing data between ViewControllers in Swift Hey, so I have a basic app in which I'm presenting a view controller from another. However, I'm trying to figure out how to pass data between both (in this case, one string).

3 ways to pass data between view controllers (forth and back), Either pass your data between your view controllers in the prepareforsegue method or create a singleton object to hold your data for the lifetime of the app. Good luck!

ViewBag uses the dynamic feature that was added in C# 4.0. We can say ViewBag=ViewData + Dynamic wrapper around the ViewData dictionary. Let's see how it is used. Pass Data Between Controller and View. In the above image, you can see how data flows from the "Controller" to the "View", and how it looks in the browser. Pass Data Between View to View

Become a Member on TheCodex for FREE and jumpstart your career - https://thecodex.me Hey Guys! Welcome to my latest video passing data between View Controlle

Comments
  • Using the Delegate-pattern
  • How? I don't quite understand?
  • check this: stackoverflow.com/questions/19343519/…
  • Possible duplicate of Pass data back to previous viewcontroller
  • You can use closures to solve that. Refer stackoverflow.com/a/56075711/5716829
  • I can't do vc.delegate = self "Value of type 'CategoryViewController' has no member 'delegate'"
  • replace PreviousVC with your own name and see edit of CategoryViewController
  • Why then during the transition the data does not change ... let currentCell = categories[indexPath.row] delegate?.defaultTransaction.category = currentCell navigationController?.popViewController(animated: true)
  • verified delegate is not nil with delegate!
  • What do you mean?