How to Pass Data to Another Controller on Dismiss Viewcontroller

How to pass data to another controller on dismiss ViewController?

The best way to pass data back to the previous view controller is through delegates... when going from ViewController A to B, pass view controller A as a delegate and on the viewWillDisappear method for ViewController B, call the delegate method in ViewController A.. Protocols would help define the delegate and the required methods to be implemented by previous VC. Here's a quick example:

Protocol for passing data:

protocol isAbleToReceiveData {
func pass(data: String) //data: string is an example parameter
}

Viewcontroller A:

class viewControllerA: UIViewController, isAbleToReceiveData {

func pass(data: String) { //conforms to protocol
// implement your own implementation
}

prepare(for: Segue) {
/** code for passing data **/
let vc2 = ViewCOntrollerB() /
vc2.delegate = self //sets the delegate in the new viewcontroller
//before displaying
present(vc2)
}
}

Dismissing viewcontroller:

class viewControllerB: UIViewController {

var delegate: isAbleToReceiveData

viewWillDisappear {
delegate.pass(data: "someData") //call the func in the previous vc
}
}

How to pass data from modal view controller back when dismissed

Depending on the data you want to pass, you can create a property in the presenting view controller, which you can set when dismissing the modal view controller, so you can spare yourself the delegate.

For example, you have a ContactsViewController, holding a var contacts: [Contact] = [] property. When you want to create a new contact, you present a modal view controller with the different values you need to create a new Contact object. When you are done and want to dismiss the view controller, you call the function as you did in your code, but set the property in the ContactsViewController. It will look something like this:

@IBAction func dismissViewController(_ sender: UIBarButtonItem) {
if let presenter = presentingViewController as? ContactsViewController {
presenter.contacts.append(newContact)
}
dismiss(animated: true, completion: nil)
}

If you don't want to use a delegate, this is how you go about it:

In your OOTDListViewController :

var testValue: String = ""

@IBAction func printReceivedValue(_ sender: UIButton) {
print(testValue)
}

In your modal view controller (I'll call it PresentedViewController) :

@IBAction func dismissViewController(_ sender: UIBarButtonItem) {
// if your OOTDListViewController is part of a UINavigationController stack, this check will probably fail.
// you need to put a breakpoint here and check if the presentingViewController is actually a UINavigationController.
// in that case, you will need to access the viewControllers variable and find your OOTDListViewController
if let presenter = presentingViewController as? OOTDListViewController {
presenter.testValue = "Test"
}
dismiss(animated: true, completion: nil)
}

If you want to use a delegate, this is how to do it:

In your OOTDListViewController:

protocol ModalDelegate {
func changeValue(value: String)
}

class OOTDListViewController: ModalDelegate {

var testValue: String = ""
@IBAction func presentViewController() {
// here, you either create a new instance of the ViewController by initializing it, or you instantiate it using a storyboard.
// for simplicity, I'll use the first way
// in any case, you cannot use a storyboard segue directly, bevause you need access to the reference of the presentedViewController object
let presentedVC = PresentedViewController()
presentedVC.delegate = self
present(presentedVC, animated: true, completion: nil)
}

func changeValue(value: String) {
testValue = value
print(testValue)
}
}

In your PresentedViewController:

class PresentedViewController {
var delegate: ModalDelegate?
var testValue: String = ""

@IBAction func dismissViewController(_ sender: UIBarButtonItem) {
if let delegate = self.delegate {
delegate.changeValue(testValue)
}
dismiss(animated: true, completion: nil)
}

}

how pass data between view controller and TableViewController when using dismiss page that opened with performSegue in swift 4?

Add to your secondViewController source code file:

protocol SecondViewControllerDelegate {

func submitButtonPushedWithText(_ text: String)
}

Add to class secondViewController property:

var delegate: SecondViewControllerDelegate?

Then conform your first controller to SecondViewControllerDelegate and implement method submitButtonPushedWithText(:):

class FirstViewController: UIViewController, SecondViewControllerDelegate {

func submitButtonPushedWithText(_ text: String) {
// use text from textField of second controller
}
}

Also setup delegate property of second controller before presenting:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let vc = segue.destination as? secondViewController {
vc.showPageType = self.checkEdit
// setup delegate
vc.delegate = self
}

Now you can call method submitButtonPushedWithText(_ text: String) in your Second controller just before calling dismiss(animated: false, completion: nil):

func submitButtonPushed() {
delegate?.submitButtonPushedWithText(textField.text!)
dismiss(animated: false, completion: nil)
}

Passing data between a ViewController to another ViewController with navigation controller Swift 5

If you are using segue, then add "segue_identifier" in storyboard and the in secondviewcontroller add:

 override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "segue_ identifier" {
let mainTab = segue.destination as! tabBarController
let nav = mainTab.viewControllers[0] as! UINavigationController
let vc = nav.viewControllers.first as! HomeViewController
vc.cid = cityId
}
}

Because your segue destination is UINavigationController, you need to send data to view controller like this

how can i pass data using dismiss?

An other way it’s to use protocole and delegate which is very common.

I’ll recommend you this link, to have a good start

https://learnappmaking.com/pass-data-between-view-controllers-swift-how-to/#back-properties

Pass data between ViewControllers using dismiss

In C controller create

var dismissClosure: ((neededData) -> Void)?

In controller which presented C its controller(B) in prepareSegue need to declare this:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let controller = segue.destination as! C
controller.dismissClosure = { [weak self] neededData in
guard let `self` = self else { return }
//TODO YOUR CHANGES
}
}

When you dismiss or pop controller call this block in C controller

dismiss:

dismiss(true) {
dismissClosure?(neededData)
}

pop

dismissClosure?(neededData)
navigationController?.popViewController(animated: true)

How to pass data from modal view controller back when dismissed

Depending on the data you want to pass, you can create a property in the presenting view controller, which you can set when dismissing the modal view controller, so you can spare yourself the delegate.

For example, you have a ContactsViewController, holding a var contacts: [Contact] = [] property. When you want to create a new contact, you present a modal view controller with the different values you need to create a new Contact object. When you are done and want to dismiss the view controller, you call the function as you did in your code, but set the property in the ContactsViewController. It will look something like this:

@IBAction func dismissViewController(_ sender: UIBarButtonItem) {
if let presenter = presentingViewController as? ContactsViewController {
presenter.contacts.append(newContact)
}
dismiss(animated: true, completion: nil)
}

If you don't want to use a delegate, this is how you go about it:

In your OOTDListViewController :

var testValue: String = ""

@IBAction func printReceivedValue(_ sender: UIButton) {
print(testValue)
}

In your modal view controller (I'll call it PresentedViewController) :

@IBAction func dismissViewController(_ sender: UIBarButtonItem) {
// if your OOTDListViewController is part of a UINavigationController stack, this check will probably fail.
// you need to put a breakpoint here and check if the presentingViewController is actually a UINavigationController.
// in that case, you will need to access the viewControllers variable and find your OOTDListViewController
if let presenter = presentingViewController as? OOTDListViewController {
presenter.testValue = "Test"
}
dismiss(animated: true, completion: nil)
}

If you want to use a delegate, this is how to do it:

In your OOTDListViewController:

protocol ModalDelegate {
func changeValue(value: String)
}

class OOTDListViewController: ModalDelegate {

var testValue: String = ""
@IBAction func presentViewController() {
// here, you either create a new instance of the ViewController by initializing it, or you instantiate it using a storyboard.
// for simplicity, I'll use the first way
// in any case, you cannot use a storyboard segue directly, bevause you need access to the reference of the presentedViewController object
let presentedVC = PresentedViewController()
presentedVC.delegate = self
present(presentedVC, animated: true, completion: nil)
}

func changeValue(value: String) {
testValue = value
print(testValue)
}
}

In your PresentedViewController:

class PresentedViewController {
var delegate: ModalDelegate?
var testValue: String = ""

@IBAction func dismissViewController(_ sender: UIBarButtonItem) {
if let delegate = self.delegate {
delegate.changeValue(testValue)
}
dismiss(animated: true, completion: nil)
}

}


Related Topics



Leave a reply



Submit