gpt4 book ai didi

ios - 选择器 View 函数未调用

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

我正在制作一个带有一些文本字段的应用程序,一旦按下,文本字段应该会从底部弹出一个选择器 View ,并且应该显示我添加的一些数组,但不知何故,放完打印语句后,我发现了最重要的两个函数,

func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? 

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)

我在选择器 View 上放置文字的代码无法运行。我该如何解决?谢谢

import UIKit

class ViewController: UIViewController{
var blocking = [Automation.blockTime.quarterHour, .halfHour, .hour, .twoHour]
var blockingString = ["Quarter Hour", "Half Hour", "Hour", "Two Hours"]
var startTimeString = ["5:00AM","6:30AM","5:30AM","6:00AM","7:00AM","7:30AM","8:00AM","8:30AM","9:00AM","9:30AM","10:00AM"]
var endTimeString = ["5:00PM","6:30PM","5:30PM","6:00PM","7:00PM","7:30PM","8:00PM","8:30PM","9:00PM","9:30PM","10:00PM"]
var startTime = [300, 390, 330, 360, 420, 450, 480, 510, 540, 570, 600]
var endTime = [Int]()

@IBOutlet weak var block: UITextField!
@IBOutlet weak var endOfSchedule: UITextField!
@IBOutlet weak var StartOfSchedule: UITextField!
@IBOutlet weak var AppointmentTextField: UITextField!
@IBOutlet weak var prorityTextField: UITextField!
@IBOutlet weak var GoButton: UIButton!

override func viewDidLoad() {
super.viewDidLoad()
createPicker()
createToolbar()
for EachElemnt in startTime {
endTime.append(EachElemnt + (12 * 60))
}
}

func createPicker() {
let dayPicker = UIPickerView()
dayPicker.delegate = self

endOfSchedule.inputView = dayPicker
StartOfSchedule.inputView = dayPicker
block.inputView = dayPicker

//Customizations
dayPicker.backgroundColor = UIColor.darkGray
}

func createToolbar() {
let toolBar = UIToolbar()
toolBar.sizeToFit()

//Customizations
toolBar.barTintColor = .black
toolBar.tintColor = .white

let doneButton = UIBarButtonItem(title: "Done", style: .plain, target: self, action: #selector(ViewController.dismissKeyboard))

toolBar.setItems([doneButton], animated: false)
toolBar.isUserInteractionEnabled = true

prorityTextField.inputAccessoryView = toolBar
}

@objc func dismissKeyboard() {
view.endEditing(true)
}
}

extension ViewController: UIPickerViewDelegate, UIPickerViewDataSource {
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}

func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
var returnment = Int()
if pickerView == endOfSchedule {
returnment = endTimeString.count
return endTimeString.count
}else if pickerView == StartOfSchedule {
returnment = startTimeString.count
return startTimeString.count
}else if pickerView == block {
returnment = blocking.count
return blocking.count
}

return returnment

func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {

var returnment = String()
if pickerView == endOfSchedule {
returnment = endTimeString[row]

}else if pickerView == StartOfSchedule {
returnment = startTimeString[row]

}else if pickerView == block {
returnment = blockingString[row]

}
print(returnment)
return returnment
}

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if pickerView == endOfSchedule {
endOfSchedule.text = endTimeString[row]
}else if pickerView == StartOfSchedule {
StartOfSchedule.text = startTimeString[row]
}else if pickerView == block {
block.text = blockingString[row]
}
}

func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
var label: UILabel

if let view = view as? UILabel {
label = view
} else {
label = UILabel()
}

label.textColor = .white
label.textAlignment = .center
label.font = UIFont(name: "Menlo-Regular", size: 17)
if pickerView == endOfSchedule {
label.text = endOfSchedule.text
}else if pickerView == StartOfSchedule {
label.text = StartOfSchedule.text
}else if pickerView == block {
label.text = block.text
}

return label
}
}
}

最佳答案

您有几个问题。

  1. 您从未设置选择器的dataSource。添加dayPicker.dataSource = self
  2. 您已经实现了 titleForRowviewForRow。仅实现其中之一。
  3. 您的任何 if 语句都不会成功,因为您正在将 UIPickerView 变量与 UITextField 实例进行比较。我很惊讶你的代码竟然能编译。

要解决最后一个问题,您需要设置所有文本字段的委托(delegate)。然后实现 textFieldDidBeginEditing 委托(delegate)方法。在其中,为文本字段设置一个属性。然后,在所有选取器 View 方法中,将该文本字段属性与文本字段导出进行比较,以便了解哪个文本字段是当前文本字段。或者检查每个文本字段是否是第一响应者。

示例:

func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
if endOfSchedule.isFirstResponder {
return endTimeString.count
} else if StartOfSchedule.isFirstResponder {
return startTimeString.count
} else {
return blocking.count
}
}

最后一件事,确保您没有将其中一些方法放入另一个方法中。您发布的代码在错误的位置有一些花括号。

关于ios - 选择器 View 函数未调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51490365/

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