popovercontroller中的Swift datepicker没有更新正确的标签

时间:2016-01-02 14:33:22

标签: swift datepicker uipopovercontroller

在我的应用程序中,我添加了2个标签和2个按钮,当用户选择相应标签应该更改的日期时,按钮被挂钩到popovercontroller中弹出日期选择器的功能。

除标签一起更新外,一切正常,我想要的只更新相关标签,即当按下button1时,label1应该收到datepicker日期,反之亦然。

这是我的代码:

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

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

1 个答案:

答案 0 :(得分:1)

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

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

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