How to Pass Values from a Pop Up View Controller to the Previous View Controller

How to pass values from a Pop Up View Controller to the Previous View Controller?

You can achieve this by either using delegate or completion handler.

Just create a delegate to handle your data on dismissing the second VC.

**

OR

**

Write a completion handler closure to get back those values in your first view controller.

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 send data from popover view controller to main view controller. Pass a string so that i can make that as the label text in the main view

From Popup View, Data send to Main ViewController using protocol in Swift 3.
enter image description here
Complete Details are given below...
1. View Controller Implementing with Protocol named sendDataToViewProtocol.

import UIKit
class ViewController: UIViewController,sendDataToViewProtocol {

@IBOutlet weak var lshowDataLabel: UILabel!

override func viewDidLoad() {
super.viewDidLoad()

}
@IBAction func btnShowPopUpDialog(_ sender: Any) {
let popUpVc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "PopupVIewController") as! PopupVIewController
//Don't forget initialize protocal deletage
popUpVc.delegate = self
self.addChildViewController(popUpVc)
popUpVc.view.frame = self.view.frame
self.view.addSubview(popUpVc.view)
popUpVc.didMove(toParentViewController: self)
}

func inputData(data: String) {
lshowDataLabel.text = data

}
}

  1. Popup View Controller With Protocol named sendDataToViewProtocol below.
    3.protocol declare outside the PopupVIewController.
  2. Don't forget to assign ViewController to PopupVIewController .
  3. In viewController withIdentifier: "PopupVIewController" , "PopupVIewController" is PopupVIewController storyborad Id.
  4. Please see the attached image.

     import UIKit

    protocol sendDataToViewProtocol {
    func inputData(data:String)
    }

    class PopupVIewController: UIViewController {
    //Protocol object
    var delegate:sendDataToViewProtocol? = nil

    @IBOutlet weak var txtInputFieldText: UITextField!
    override func viewDidLoad() {
    super.viewDidLoad()
    self.view.backgroundColor = UIColor
    .black.withAlphaComponent(0.8)
    }

    @IBAction func btnSendDataToViewController(_ sender: Any) {
    //"Check Delegate nil"
    if(delegate != nil){
    //Check textField is empty
    if(txtInputFieldText.text != ""){
    //set textField Data to protocol Function
    delegate?.inputData(data: txtInputFieldText.text!)
    self.view.removeFromSuperview()
    }

    }
    }

    @IBAction func btnClose(_ sender: Any) {
    self.view.removeFromSuperview()
    }

    }

Passing variables back from a ViewController to a previous one, but variables not updating?

Your second view controller is instantiating a new instance of the first view controller rather than using the instance that was already there. The second view controller shouldn’t present the first view controller again, but rather dismiss (or pop) back to it, depending upon the first presented or pushed to it.

By the way, the delegate property of the second view controller that points back to the first one should be a weak property. You never want a child object maintaining a strong reference to a parent object. Besides, delegates are almost always weak...

Pass Values from Popup VC to controller

You can use Delegate pattern or Clouser callback handler to pass a value back to the parent view.

here is a example:

Define a clouser in your popVC like this:

var clouserName: ((returnType) -> Void)?

Inside your popVC where you need to call the clouser:

clouserName?(returnValue)

in your parent controller, capture the value in this way:

vc.clouserName = { returnValue in // dont forget [weak self] if you need self
// Do your stuff here
}

Pass data back to previous viewcontroller

You can use a delegate. So in your ViewController B you need to create a protocol that sends data back to your ViewController A. Your ViewController A would become a delegate of ViewController B.

If you are new to objective C, please look at What is Delegate.

Create protocol in ViewControllerB.h :

#import <UIKit/UIKit.h>

@protocol senddataProtocol <NSObject>

-(void)sendDataToA:(NSArray *)array; //I am thinking my data is NSArray, you can use another object for store your information.

@end

@interface ViewControllerB : UIViewController

@property(nonatomic,assign)id delegate;

ViewControllerB.m

@synthesize delegate;
-(void)viewWillDisappear:(BOOL)animated
{
[delegate sendDataToA:yourdata];

}

in your ViewControllerA : when you go to ViewControllerB

ViewControllerA *acontollerobject=[[ViewControllerA alloc] initWithNibName:@"ViewControllerA" bundle:nil];
acontollerobject.delegate=self; // protocol listener
[self.navigationController pushViewController:acontollerobject animated:YES];

and define your function:

-(void)sendDataToA:(NSArray *)array
{
// data will come here inside of ViewControllerA
}

Edited :

You can See this example : How you can Pass data back to previous viewcontroller: Tutorial link



Related Topics



Leave a reply



Submit