Swift popovercontroller 中的日期选择器没有更新正确的标签

Swift datepicker in popovercontroller not updating the correct label

在我的应用程序中,我添加了 2 个标签和 2 个按钮,这些按钮连接到一个函数,该函数在 popovercontroller 中弹出一个日期选择器,当用户 select 一个日期时,相应的标签应该改变。

除标签一起更新外,一切正常,我想要的是仅更新相关标签,即当按下按钮 1 时,标签 1 应该接收日期选择器日期,反之亦然。

这是我的代码:

import UIKit

class PopoverTestVC: UIViewController, UIPopoverPresentationControllerDelegate {

@IBOutlet weak var dateLabel: UILabel!
@IBOutlet weak var dateLabel2: UILabel!

@IBOutlet weak var selectButton: UIButton!
@IBOutlet weak var selectButton2: UIButton!

@IBOutlet var datePicker: UIDatePicker!

var dateString = ""

let vc = UIViewController()

override func viewDidLoad() {
    super.viewDidLoad()

    datePicker.timeZone = NSTimeZone(abbreviation: "GMT")
    //datePicker.addTarget(self, action: Selector("datePicked:"), forControlEvents: UIControlEvents.ValueChanged)
}

func datePicked(sender: UIDatePicker) {
    let dateFormatter = NSDateFormatter()
    dateFormatter.dateFormat = "dd MMM yyyy HH:mm"
    dateFormatter.timeZone = NSTimeZone(abbreviation: "GMT")
    dateLabel.text = dateFormatter.stringFromDate(datePicker.date)

}

func datePicked2(sender: UIDatePicker) {
    let dateFormatter = NSDateFormatter()
    dateFormatter.dateFormat = "dd MMM yyyy HH:mm"
    dateFormatter.timeZone = NSTimeZone(abbreviation: "GMT")
    dateLabel2.text = dateFormatter.stringFromDate(datePicker.date)

}

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

@IBAction func popUp(sender: UIButton)
{
    let fr = datePicker.frame
    vc.view.frame = fr
    vc.view.addSubview(datePicker!)
    vc.view.backgroundColor = UIColor.greenColor()
    vc.preferredContentSize = datePicker.frame.size
    vc.modalPresentationStyle = UIModalPresentationStyle.Popover

    if sender == selectButton
    {
        datePicker.addTarget(self, action: Selector("datePicked:"), forControlEvents: UIControlEvents.ValueChanged)
    }
    else if sender == selectButton2
    {
        datePicker.addTarget(self, action: Selector("datePicked2:"), forControlEvents: UIControlEvents.ValueChanged)
    }

    let popOverPC = vc.popoverPresentationController
    vc.popoverPresentationController?.sourceRect = sender.frame
    vc.popoverPresentationController?.sourceView = view
    popOverPC!.permittedArrowDirections = UIPopoverArrowDirection.Any
    popOverPC!.delegate = self
    presentViewController(vc, animated: true, completion: nil)
}

func adaptivePresentationStyleForPresentationController(controller: UIPresentationController) -> UIModalPresentationStyle
{
    return UIModalPresentationStyle.None
}
}

感谢任何帮助或建议以更好的方式做到这一点,因为该应用程序可能有多个使用相同日期选择器的网点。

我会将你的日期选择器移动到自定义 class(这也让你可以使用 xib 来做一些很酷的设计)从你的视图控制器调用它,为调用的按钮设置一个 var日期选择器。在日期选择器中创建一个协议,并在父视图控制器中实现委托,根据调用日期选择器的按钮填充您需要的任何标签。

例如,在我的应用程序中,我有几个必须包含日期的文本字段。当用户开始编辑文本字段时,我从 textFieldShouldBeginEditing 启动自定义弹出窗口并将文本字段分配给 var.
在我的自定义日期选择器中,父视图控制器被设置为协议的委托:

// MARK: - Textfield delegate
func textFieldShouldBeginEditing(textField: UITextField) -> Bool {
    textField.backgroundColor = UIColor.whiteColor()

    if (textField.tag == 500 || textField.tag == 501)
    {
        activeTextField = textField
        let vc = BGSDatePickerVC(nibName: "BGSDatePickerVC", bundle: nil)
        vc.modalPresentationStyle = UIModalPresentationStyle.Popover
        if textField.tag == 500{
            vc.popoverPresentationController?.sourceView = viewMarkerFromDate
        }else
        {
            vc.popoverPresentationController?.sourceView = viewMarkerToDate
        }

        vc.popoverPresentationController?.permittedArrowDirections = UIPopoverArrowDirection.Up
        vc.preferredContentSize = CGSizeMake(vc.view.frame.width, vc.view.frame.height)
        vc.delegate = self
        self.presentViewController(vc, animated: true, completion: nil)
        return false
    }
    return true       
}

我的自定义日期选择器弹出窗口中的协议:

protocol BGSDatePickerVCProtocol
{
    func bgsDatePickerUse(date: NSDate, strDate: String)
}

class BGSDatePickerVC: UIViewController {
    var delegate:BGSDatePickerVCProtocol! = nil

以及调用视图控制器中的实现:

// MARK: - Delegates
// MARK: BGSDatePickerVCProtocol

func bgsDatePickerUse(date: NSDate, strDate: String)
{
    if activeTextField.tag == 500
    {
        dateSelectedFrom = date
    }
    if activeTextField.tag == 501
    {
        dateSelectedTo = date
    }
    activeTextField.text = strDate
}