如何使用委托和协议不使用从编程创建的UIPopoverController到ViewController的segue传递数据?

时间:2015-09-08 13:27:31

标签: ios swift delegates protocols uipopovercontroller

我有一个问题,我无法管理该怎么做。问题是我已经以编程方式从下面显示的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中传递CustomerCustomer委托方法定义;

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

    }

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

我已经查看了下面的链接,但我不能说我已经清楚地知道了...... Trying to understand protocol/delegates in Swift

1 个答案:

答案 0 :(得分:1)

我确定您需要分配delegate 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 app赢了,那就崩溃了。如果您完全确定委托永远不会为零,那么最好将其声明为var delegate : PassValues!