我正在尝试在页面上使用多个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
方法中使用该实例,然后更改相应的文本字段的值?我试着检查textfield是否是firstResponder,甚至试图检查UIPicker本身,但似乎不可能。我是否需要改变我的方法,如果是这样的话?
更新
我添加了现有的UIPicikerView
委托方法。
为了澄清,我想在用户按下“完成”时将UIPicker
的值分配给正确的文本字段,因此如果用户想要从UIPicker中选择第一行,它会得到插入。
答案 0 :(得分:4)
您可以实现以下UIPickerViewDelegate的方法:
optional func pickerView(_ pickerView: UIPickerView,
didSelectRow row: Int,
inComponent component: Int)
当用户选择组件中的行并使用
时,选择器视图调用该方法pickerView
(表示请求数据的选择器视图的对象)知道哪个选择器视图已被使用。
答案 1 :(得分:2)
您始终可以使用try {
var threads = fs.readFileSync('./database/threadList.txt');
var threadIds = threads.toString().split('\n');
for(i in threadIds) {
threadArray[i] = threadIds[i].split(",");
}
console.log("This works: " + threadArray[0]);
} catch (e) {
console.error("Error reading file: ", e);
}
的{{1}}属性传递数据:
UIView
答案 2 :(得分:0)
您可以尝试使用您设置为pickerview的数据源的不同条件动态更改数据
答案 3 :(得分:0)
也许您不需要确定它是哪个选择器,而是确定哪个文本字段。
在pickerDoneAction(sender:)
中,使用let textField = sender.superview.superview
,其中sender
为按钮,parent
为工具栏,工具栏的parent
为文本字段。然后用pickerData[0]
填充它。
实现相同目标的另一种方法是使用UITextFieldDelegate
的方法textFieldDidEndEditing(_:)
。当文本字段结束编辑时,在那里插入默认值。
答案 4 :(得分:0)
let pickerData: [String] = ["1", "2", "3", "4"]
var picker = UIPickerView()
let pickerData2: [String] = ["A", "B", "C", "D"]
var picker2 = UIPickerView() // <<<<<<<<<<<<<
var userPicker:UIPickerView?
@IBOutlet weak var textField: UITextField!
@IBOutlet weak var textField2: UITextField!
.....
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
userPicker = pickerView // <<<<<<<<<<<<<
if pickerView == picker {
return pickerData[row]
} else return pickerData2[row]
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
userPicker = pickerView // <<<<<<<<<<<<<
if pickerView == picker {
textField.text = pickerData[row]
} else textField2.text = pickerData2[row]
}
答案 5 :(得分:0)
您可以简单地检查哪个文本字段处于编辑状态。在完成的单击功能中执行此操作。您不必在pickerView或textField中添加任何标签。
func pickerDoneAction(sender: UIBarButtonItem) {
if textField1.isEditing == true {
Print("pickerview 1 is selected")
} else {
Print("pickerview 2 is selected")
}
}
希望这会有所帮助! :)