0

I have an issue that I couldn't manage how to do. The issue is that I have programmatically created UIPopoverController calling from FirstViewController shown below.

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

And passCustomer delegate method defination in FirstViewController ;

    func passCustomer(customer: Customer) {

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

And AddCustomerPopoverVC shown below ;

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

    }

When I tapped to my save button it does nothing. I mean it does sth. but it does nothing about the passing value process. What should I do for the complete this process. What am I doing wrong or missed ?

I've already check the link below but I can't say that I've got it clearly.. Trying to understand protocol/delegates in Swift

Community
  • 1
  • 1
letitbefornow
  • 427
  • 6
  • 20

1 Answers1

1

I'm pretty sure you need to assign delegate of popView and not 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)
 }

Also it's a good practice to call optional delegates methods like this (notice question mark instead of exclamation):

delegate?.passCustomer(customer)

So if delegate is nil app won't crash. If you are completely sure that delegate will never be nil then you better declare it as var delegate : PassValues!.

glyuck
  • 3,367
  • 18
  • 14