Swift, Pass Data Back from Popover to View Controller

Swift, Pass data back from popover to view controller

You can use delegate(protocol) methods to send back data to previous view controller.

IN CURRENT VC:

protocol MyProtocol: class
{
func sendArrayToPreviousVC(myArray:[AnyObject])
}

Make a var in your class.

weak var mDelegate:MyProtocol?

Now call the protocol method when you pop the view controller, with your "properties" array as parameter.

mDelegate?.sendArrayToPreviousVC(properties)

IN PREVIOUS VC:

In your previous VC, set the mDelegate property to self, when you push the current VC.

currentVC.mDelegate = self
//PUSH VC

Now implement the protocol method in your previous VC.

func sendArrayToPreviousVC(myArray:[AnyObject]) {
//DO YOUR THING
}

Pass data back from a popover view controller on iPhone

In prepareForSegue, the destinationViewController is your PopoverViewController. You need to cast it to that and set the delegate on that so that you can pass back data, and you need to set the popoverPesentationController?.delegate as well. You don't need the rest of the code in prepareForSegue:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// popover segue
if segue.identifier == "popoverSegue" {
let popoverViewController = segue.destinationViewController as! PopoverViewController
popoverViewController.delegate = self
popoverViewController.popoverPresentationController?.delegate = self
}
}

Swift, Pass data from popover controller to previous controller

You can use custom delegate method to fill the value.

class PopupviewController {

weak var delegate: filleTextViewDelegate?

func calldelegate () {
delegate?.fillTextview(filledData)
}
}
protocol filleTextViewDelegate : class {
func fillTextview(filledData: String)
}
class CurrentViewController :filleTextViewDelegate {

func fillTextview(filledData: String) {
textView.text = filledData
}

}

Pass data to and from Popover in Swift

class ViewController: UIViewController,SavingViewControllerDelegate,UIPopoverPresentationControllerDelegate{

@IBOutlet var labelText: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}

@IBAction func buttonPopOverClick(sender: UIButton)
{
let savingsInformationViewController = storyboard?.instantiateViewControllerWithIdentifier("SavingsAddPopoverVC") as SavingViewController

savingsInformationViewController.delegate = self
savingsInformationViewController.strSaveText=labelText.text

savingsInformationViewController.modalPresentationStyle = .Popover
if let popoverController = savingsInformationViewController.popoverPresentationController {
popoverController.sourceView = sender
popoverController.sourceRect = sender.bounds
popoverController.permittedArrowDirections = .Any
popoverController.delegate = self
}
presentViewController(savingsInformationViewController, animated: true, completion: nil)
}

func saveText(strText: NSString) {
labelText.text=strText
}

// MARK: - UIPopoverPresentationControllerDelegate
func adaptivePresentationStyleForPresentationController(controller: UIPresentationController!) -> UIModalPresentationStyle {
return .FullScreen
}

func presentationController(controller: UIPresentationController!, viewControllerForAdaptivePresentationStyle style: UIModalPresentationStyle) -> UIViewController! {
return UINavigationController(rootViewController: controller.presentedViewController)
}
}

protocol SavingViewControllerDelegate
{
func saveText(var strText : NSString)
}

class SavingViewController: UIViewController {
@IBOutlet var textField: UITextField!
var delegate : SavingViewControllerDelegate?
var strSaveText : NSString!
override func viewDidLoad() {
super.viewDidLoad()
textField.text = strSaveText
// Do any additional setup after loading the view.
}

@IBAction func buttonDone(sender: UIButton)
{
if (self.delegate) != nil
{
delegate?.saveText(textField.text)
self.dismissViewControllerAnimated(true, nil)
}
}
}

Can't pass datas back from UIView Popover Dismiss

oky so you can do it using unwind segue here is sample project :

sample projecct

process :

Add this method to SearchBarsController below viewWillAppear

@IBAction func unWindFromFilterViewController(_ sender: UIStoryboardSegue) {

}

Than go to Storyboard and go to SearchFilterViewController and then cntrl + Drag from DismissPopoverOnClic to top of the exit button then select unWindFromFilterViewController .

Drag button to this exit and select unWindFromFilterViewController

Than this the SearchFilterViewController write this method for passing data

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let destVC = segue.destination as? ViewController {
destVC.testValue = "Test"

}
}

You will get your desired data back . thanks

Swift, pass data from dynamic cell row selection to popover view controller

Here is how you pass data

class CalculadoraViewController: UIViewController {

@IBOutlet weak var montoUSD: UITextField!
@IBOutlet weak var montoCRC: UITextField!
@IBOutlet weak var prVenta: UILabel!
@IBOutlet weak var prCompra: UILabel!
var prVentaText:String!
var prCompraText:String!

override func viewDidLoad() {

prVenta.text = prVentaText
prCompra.text = prCompraText

montoUSD.placeholder = montoCRC.text
super.viewDidLoad()
}
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
let vc = storyboard?.instantiateViewController(identifier: "calculadora") as! CalculadoraViewController

vc.prCompraText = String(precioCompra[indexPath.row])
vc.prVentaText = String(precioVenta[indexPath.row])

present(vc, animated: true)

}

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()
    }

    }

Pass data from presented View in Navigation Controller, back to custom view (XIB)

You can use one in three ways:

  • NSNotificationCenter
  • Delegate
  • Closure


Related Topics



Leave a reply



Submit