我是ios开发和swift的新手。我遇到了一个问题。请帮我解决这个问题。我正在使用一个自定义日期选择器,因为我使用两个标签,标签名称是fromDateLbl和toDateLbl。我正在使用来自DateBtn和toDateBtn的两个按钮。如果我使用一个标签而一个按钮无法获得两个按钮的日期,我可以获得约会。
请参阅我的代码。
import UIKit
class SecondViewController: UIViewController {
@IBOutlet var fromDateLabel: UILabel!
@IBOutlet var toDateLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func fromDateAction(_ sender: Any) {
let popOverVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "CustomDatePickerViewController") as! CustomDatePickerViewController
self.addChildViewController(popOverVC)
self.view.addSubview(popOverVC.view)
popOverVC.view.frame = view.bounds
popOverVC.delegate = self as? CustomDatePickerDelegate
popOverVC.didMove(toParentViewController: self)
self.navigationController?.navigationBar.isHidden = true
}
}
@IBAction func toDateAction(_ sender: Any) {
let popOverVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "CustomDatePickerViewController") as! CustomDatePickerViewController
self.addChildViewController(popOverVC)
self.view.addSubview(popOverVC.view)
popOverVC.view.frame = view.bounds
popOverVC.delegate = self as? CustomDatePickerDelegate
popOverVC.didMove(toParentViewController: self)
self.navigationController?.navigationBar.isHidden = true
}
}
extension SecondViewController:CustomDatePickerDelegate
{
func CustomDateSeletced(value: String) {
fromDateLabel.text = value
}
}
secondVc code。
class CustomDatePickerViewController: UIViewController {
@IBOutlet var myDatePicker: UIDatePicker!
var delegate: CustomDatePickerDelegate?
override func viewDidLoad() {
super.viewDidLoad()
view.frame.size.height = UIScreen.main.bounds.height
self.view.backgroundColor = UIColor.black.withAlphaComponent(0.6)
}
@IBAction func saveDateAction(_ sender: Any) {
myDatePicker.datePickerMode = UIDatePickerMode.date
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd MMM yyyy"
let saveDate = dateFormatter.string(from: myDatePicker.date)
print("selectedDate",saveDate)
delegate?.CustomDateSeletced(value: saveDate)
self.removeAnimate()
self.navigationController?.navigationBar.isHidden = false
}
这是我的委托班。
import Foundation
protocol CustomDatePickerDelegate {
func CustomDateSeletced(value: String)
}
答案 0 :(得分:0)
这不是完美的解决方案,但下面的代码可能会对您有所帮助。 为了改善一点,您可以使用UILabel操作并删除UIButtons并存储已编辑的标签,这样您也可以阻止UILabel编辑。
Phillip Mills为您实施了更好的解决方案。
class SecondViewController: UIViewController {
@IBOutlet var fromDateLabel: UILabel!
@IBOutlet var toDateLabel: UILabel!
@IBOutlet var fromDateButton: UIButton!
@IBOutlet var toDateButton: UIButton!
var selectedButton: UIButton?
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func selectDateAction(_ sender: Any) {
selectedButton = sender as? UIButton
let popOverVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "CustomDatePickerViewController") as! CustomDatePickerViewController
self.addChildViewController(popOverVC)
popOverVC.delegate = self as? CustomDatePickerDelegate
present(popOverVC, animated: true, completion: nil)
self.navigationController?.navigationBar.isHidden = true
}
}
extension SecondViewController:CustomDatePickerDelegate
{
func CustomDateSeletced(value: String) {
if let button = selectedButton {
if button == toDateButton {
toDateLabel.text = value
} else {
fromDateLabel.text = value
}
selectedButton = nil
}
}
}
-
@IBAction func saveDateAction(_ sender: Any) {
myDatePicker.datePickerMode = UIDatePickerMode.date
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd MMM yyyy"
let saveDate = dateFormatter.string(from: myDatePicker.date)
delegate?.CustomDateSeletced(value: saveDate)
self.navigationController?.navigationBar.isHidden = false
dismiss(animated: true, completion: nil)
}
答案 1 :(得分:0)
因为您的代理人不记得调用选择器的原因,您可能会遇到委托协议可能不是最佳解决方案的情况。这是一个非常简化的版本,您可以使用闭包来完成此操作。它是一种策略,允许选择器具体知道如何处理它产生的值。
class Second {
@IBOutlet var fromDateLabel: UILabel!
@IBOutlet var toDateLabel: UILabel!
@IBAction func fromDateAction(_ sender: Any) {
// Setup popover as before
let picker = Picker()
picker.useCompletion { (returnedDate) in
self.fromDateLabel.text = returnedDate
}
}
@IBAction func toDateAction(_ sender: Any) {
// Setup popover as before
let picker = Picker()
picker.useCompletion { (returnedDate) in
self.toDateLabel.text = returnedDate
}
}
}
class Picker {
var completion: ((String)->())?
func useCompletion(_ block: @escaping (String)->()) {
completion = block
}
@IBAction func saveDateAction(_ sender: Any) {
let saveDate = "The text you want to return"
if let completion = completion {
completion(saveDate)
}
}
}