简体   繁体   English

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

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

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. 问题是我已经以编程方式创建了从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)
 }

And passCustomer delegate method defination in FirstViewController ; 并且在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
}

And AddCustomerPopoverVC shown below ; 并且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.
        }

    }

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 我已经检查了下面的链接,但我不能说清楚了。. 试图了解Swift中的协议/代理

I'm pretty sure you need to assign delegate of popView and not popController : 我很确定您需要分配popView delegate而不是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! 如果您完全确定委托永远不会为零,那么最好将其声明为var delegate : PassValues! .

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何不使用segues将数据从一个viewController传递到另一个? - How to pass data from one viewController to another without using segues? 如何使用委托将数据从第二个ViewController传递到TableViewController - How to pass data from 2nd viewcontroller to tableviewcontroller using delegates 如何使用委托将数据从 UIPageViewController 传递给子 ViewController - how to pass data from UIPageViewController to child ViewController using delegates 如何使用segues将数据传递给另一个viewcontroller的函数? - How to pass data to another viewcontroller's function using segues? 在没有Segues的ViewController之间传递数据 - Pass data between ViewController without Segues 如何将数据传递给使用 instantiateViewControllerWithIdentifier 创建的 ViewController? - How to pass Data to a ViewController created using instantiateViewControllerWithIdentifier? 使用委托/协议将数据传递给第三方视图控制器 - using delegates/protocols to pass data to third party view controller 如何在不使用segues的情况下将tableview单元格的名称传递给另一个swift文件? - How can I pass the name of a tableview cell to another swift file without using segues? 如何在UIViewControllers与协议/委托之间传递数据 - How to pass data between UIViewControllers with protocols/delegates 如何使用segue在情节提要场景之间传递信息? - How can I pass information between storyboard scenes using segues?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM