如何在不使用转场的情况下使用委托和协议将数据从以编程方式创建的 UIPopoverController 传递到 ViewController?

How can I pass data without using segues from programmatically created UIPopoverController to ViewController using delegates and protocols?

我遇到了一个我无法解决的问题。问题是我以编程方式创建了从 FirstViewController 调用的 UIPopoverController,如下所示。

@IBAction func addButton(sender: UIButton) {

            let popoverRect = CGRectMake(-360.0, 170.0, 515.0, 415.0)
            var popView = AddCustomerPopoverVC (nibName :"AddCustomerPopoverVC" , bundle : nil)
            var popController = UIPopoverController (contentViewController: popView)
            popController.delegate = self
            popController.popoverContentSize = CGSize(width: 515.0 , height: 415.0)
            popController.presentPopoverFromRect(popoverRect , inView: self.view , permittedArrowDirections: UIPopoverArrowDirection.allZeros, animated: true)
 }

并在 FirstViewController 中定义 passCustomer 委托方法;

    func passCustomer(customer: Customer) {

        self.profilePhoto.image = UIImage(named: "circleBlue.png") as UIImage?
        self.usernameLabel.text = customer.name
        self.eMailLabel.text = customer.eMail
}

和如下所示的 AddCustomerPopoverVC ;

    import UIKit
    import Foundation

    protocol PassValues {

        func  passCustomer(customer : Customer )

    }


    class AddCustomerPopoverVC: UIViewController  {

        var delegate : PassValues? = nil
        @IBOutlet weak var navBar: UINavigationBar!
        @IBOutlet weak var nameTextField: UITextField!
        @IBOutlet weak var surnameTextField: UITextField!
        @IBOutlet weak var eMailTextField: UITextField!
        @IBOutlet weak var phoneTextField: UITextField!
        @IBOutlet weak var advSwitchOutlet: UISwitch!
        @IBOutlet weak var taxFreeSwitchOutlet: UISwitch!

        @IBAction func saveButton(sender: UIBarButtonItem) {

            var cName = "\(self.nameTextField.text) \(self.surnameTextField.text)"
            var cEmail = self.eMailTextField.text
            var cPhone = self.phoneTextField.text
            var cAdDelivery = self.advSwitchOutlet.on ? true : false
            var cTaxFree = self.taxFreeSwitchOutlet.on ? true : false

            let customer : Customer = Customer(name: cName, eMail: cEmail, phone: cPhone, adDelivery: cAdDelivery, taxFree: cTaxFree)

delegate!.passCustomer(customer)

        }



        override func viewDidLoad() {
            super.viewDidLoad()

            // Do any additional setup after loading the view.
        }

        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
            // Dispose of any resources that can be recreated.
        }

    }

当我点击我的保存按钮时,它没有任何反应。我的意思是它做某事。但它对传递值过程没有任何作用。我应该怎么做才能完成这个过程。我做错了什么或错过了什么?

我已经检查了下面的 link 但我不能说我已经清楚了.. Trying to understand protocol/delegates in Swift

我很确定您需要分配 delegate of popView 而不是 popController:

@IBAction func addButton(sender: UIButton) {
        let popoverRect = CGRectMake(-360.0, 170.0, 515.0, 415.0)
        var popView = AddCustomerPopoverVC (nibName :"AddCustomerPopoverVC" , bundle : nil)
        popView.delegate = self
        var popController = UIPopoverController (contentViewController: popView)
        popController.delegate = self
        popController.popoverContentSize = CGSize(width: 515.0 , height: 415.0)
        popController.presentPopoverFromRect(popoverRect , inView: self.view , permittedArrowDirections: UIPopoverArrowDirection.allZeros, animated: true)
 }

此外,像这样调用可选的委托方法也是一个好习惯(注意问号而不是感叹号):

delegate?.passCustomer(customer)

因此,如果委托为 nil,应用程序将不会崩溃。如果您完全确定委托永远不会为零,那么您最好将其声明为 var delegate : PassValues!.