gpt4 book ai didi

ios - 确定哪个 UIPicker 的工具栏按钮发送了消息

转载 作者:可可西里 更新时间:2023-11-01 00:21:14 25 4
gpt4 key购买 nike

我想在一个页面上使用多个 UIPickers。我想我已经配置了几乎所有的东西。当我使用 UIPicker 时,我添加了一个带有“取消”和“完成”按钮的工具栏。

工具栏是使用相同的函数构建的,因此当用户点击“完成”时它会调用相同的操作。下面是一个伪示例流程。

 let pickerData: [String] = ["1", "2", "3", "4"]
var picker = UIPickerView()
let pickerData2: [String] = ["A", "B", "C", "D"]
var picker2 = UIPickerView()

@IBOutlet weak var textField: UITextField!
@IBOutlet weak var textField2: UITextField!

override func viewDidLoad() {
super.viewDidLoad()
// set up pickers
setPickerView()
}

// set the pickerview
func setPickerView(){
picker.dataSource = self
picker.delegate = self
textField.inputView = picker
textField.inputAccessoryView = initToolBar()
// second picker
picker2.dataSource = self
picker2.delegate = self
textField2.inputView = picker
textField2.inputAccessoryView = initToolBar()
}

// build the toolbar for uipicker, so a user can select a value
func initToolBar() -> UIToolbar {
let toolBar = UIToolbar()
toolBar.barStyle = UIBarStyle.default
toolBar.isTranslucent = true
toolBar.tintColor = UIColor(red:14.0/255, green:122.0/255, blue:254.0/255, alpha: 1)
toolBar.sizeToFit()
// TODO need to update actions for all buttons
let cancelButton = UIBarButtonItem(title: "Cancel", style: UIBarButtonItemStyle.plain, target: self, action: nil)
let doneButton = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.plain, target: self, action: #selector(pickerDoneAction))
let spaceButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.flexibleSpace, target: nil, action: nil)
toolBar.setItems([cancelButton, spaceButton, doneButton], animated: false)
toolBar.isUserInteractionEnabled = true

return toolBar
}

func pickerDoneAction(sender: UIBarButtonItem){
/*Here I'm just updating the same textfield when the done button is
is pressed, I'm not sure how I can get the instance of the picker into this method*/
let indexPath = picker.selectedRow(inComponent: 0)
textfield.text = data[indexPath]
}

// delegates
func numberOfComponents(in pickerView: UIPickerView) -> Int { return 1 }
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
if pickerView == picker {
return pickerData.count
} else return pickerData2.count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
if pickerView == picker {
return pickerData[row]
} else return pickerData2[row]
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if pickerView == picker {
textField.text = pickerData[row]
} else textField2.text = pickerData2[row]
}

有没有办法确定单击完成按钮时使用的是哪个 UIPicker,这样我就可以在我的 pickerDoneAction 方法中使用该实例,然后更改相应文本字段的值?我尝试检查文本字段是否为 firstResponder,甚至尝试检查 UIPicker 本身,但这似乎不可能。我是否需要改变我的方法?如果需要,如何改变?

更新我在现有的 UIPicikerView 委托(delegate)方法中添加了。

澄清一下,我希望在用户按下“完成”时将 UIPicker 的值分配给正确的文本字段,因此如果用户想从 UIPicker 中选择第一行, 它被插入。

最佳答案

您可以实现以下 UIPickerViewDelegate 的方法:

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

当用户在组件中选择一行并使用时,选择器 View 调用该方法

pickerView

(表示请求数据的选择器 View 的对象)知道使用了哪个选择器 View 。

关于ios - 确定哪个 UIPicker 的工具栏按钮发送了消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42740824/

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