使用Done按钮创建两个UIPickerView的步骤如下:
下面是一个示例代码:
import UIKit
class ViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate {
let pickerView1 = UIPickerView()
let pickerView2 = UIPickerView()
override func viewDidLoad() {
super.viewDidLoad()
pickerView1.dataSource = self
pickerView1.delegate = self
pickerView2.dataSource = self
pickerView2.delegate = self
let toolbar = UIToolbar(frame: CGRect(x: 0, y: 0, width: view.frame.size.width, height: 44))
let doneButton = UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(doneButtonTapped))
toolbar.setItems([doneButton], animated: true)
let textField1 = UITextField(frame: CGRect(x: 0, y: 100, width: view.frame.size.width, height: 44))
textField1.inputView = pickerView1
textField1.inputAccessoryView = toolbar
view.addSubview(textField1)
let textField2 = UITextField(frame: CGRect(x: 0, y: 200, width: view.frame.size.width, height: 44))
textField2.inputView = pickerView2
textField2.inputAccessoryView = toolbar
view.addSubview(textField2)
}
@objc func doneButtonTapped() {
let selectedValue1 = pickerView1.selectedRow(inComponent: 0)
let selectedValue2 = pickerView2.selectedRow(inComponent: 0)
// 处理选中的数值
print("PickerView 1: \(selectedValue1)")
print("PickerView 2: \(selectedValue2)")
// 其他处理逻辑...
view.endEditing(true)
}
// UIPickerViewDataSource方法
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
// 返回相应的行数
return 10
}
// UIPickerViewDelegate方法
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
// 返回每行的标题
return "Row \(row)"
}
}
这段代码创建了两个UIPickerView对象,并将其数据源和代理设置为当前视图控制器。在视图控制器的界面中添加了一个工具栏,并在工具栏中放置了一个Done按钮。当点击Done按钮时,会调用doneButtonTapped
方法来获取选中的UIPickerView的数值,并进行相应的处理。
注意:这只是一个示例代码,实际使用时需要根据具体需求进行适当的修改和扩展。
领取专属 10元无门槛券
手把手带您无忧上云