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

Tags:

Ios

Swift