gpt4 book ai didi

popovercontroller 中的 Swift datepicker 未更新正确的标签

转载 作者:行者123 更新时间:2023-11-30 10:08:36 25 4
gpt4 key购买 nike

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

除了标签一起更新之外,一切正常,我想要的是仅更新相关标签,即当按下按钮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
}
}

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

最佳答案

我会将您的日期选择器移动到自定义类(这也允许您使用 xib 来做一些很酷的设计工作)从您的 View Controller 调用它,将 var 设置为调用日期选择器的按钮。在日期选择器中创建一个协议(protocol),并在父 View Controller 中实现委托(delegate),根据调用日期选择器的按钮填充您需要的任何标签。

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

// 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):

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

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

以及调用 View Controller 中的实现:

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

关于popovercontroller 中的 Swift datepicker 未更新正确的标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34567008/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com