if语句中根据UIPickerView选中的行更改标签
Change Label According to UIPickerView's Selected Row in If Statement
我仔细搜索了多个页面和帖子,但无法完全找到我需要的东西。我目前正在处理一个项目,其中一个控制器有多个文本字段,输出使用单个 UIPickerView
,有时标签需要根据用户的选择进行更改。
如何根据 UIPickerView
的选定行更改标签,尤其是在 if 语句中?
我做了一个简单的测试代码和例子来演示。
代码
class TestViewController: UIViewController, UITextFieldDelegate {
@IBOutlet weak var categoryOutput: UITextField!
@IBOutlet weak var shapeOutput: UITextField!
@IBOutlet weak var lengthDiaOutput: UITextField!
@IBOutlet weak var heightOutput: UITextField!
@IBOutlet weak var dimensionLabel: UILabel!
let category = ["Perimeter", "Area"]
let shape = ["Rectangle", "Circle", "Triangle"]
// MARK: - viewDidLoad()
override func viewDidLoad() {
super.viewDidLoad()
createCategoryPicker()
createShapePicker()
}
// MARK: - UIPickerViews
// Create a UIPickerView as an input text field for 'Category'.
func createCategoryPicker() {
let categoryPicker = UIPickerView()
categoryPicker.delegate = self
categoryPicker.tag = 1
categoryOutput.inputView = categoryPicker
categoryPicker.backgroundColor = UIColor(red: 238/255.0, green: 238/255.0, blue: 238/255.0, alpha: 1.0)
}
// Create a UIPickerView as an input text field for 'Shape'.
func createShapePicker() {
let shapePicker = UIPickerView()
shapePicker.delegate = self
shapePicker.tag = 2
shapeOutput.inputView = shapePicker
shapePicker.backgroundColor = UIColor(red: 238/255.0, green: 238/255.0, blue: 238/255.0, alpha: 1.0)
}
}
// MARK: - Extensions
// Create a UIPickerView.
extension TestViewController: UIPickerViewDelegate, UIPickerViewDataSource {
// Set number of lists.
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
// Set number of rows in list.
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
if pickerView.tag == 1 {
return category.count }
if pickerView.tag == 2 {
return shape.count }
return 0
}
// Set content of list.
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
if pickerView.tag == 1 {
return category[row] }
if pickerView.tag == 2 {
return shape[row] }
return nil
}
// Grab selection and change label.
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if pickerView.tag == 1 {
categoryOutput.text = category[row] }
if pickerView.tag == 2 {
shapeOutput.text = shape[row]
if component == 1 {
dimensionLabel.text = "Diameter" }
else {
dimensionLabel.text = "Length" }
}
}
// Set appearance of contents.
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() }
if pickerView.tag == 1 {
label.text = category[row] }
if pickerView.tag == 2 {
label.text = shape[row] }
label.font = UIFont(name: "Avenir", size: 17)
label.textColor = .black
label.textAlignment = .center
return label
}
}
这是需要重点关注的部分。
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if pickerView.tag == 1 {
categoryOutput.text = category[row] }
if pickerView.tag == 2 {
shapeOutput.text = shape[row]
if component == 1 {
dimensionLabel.text = "Diameter" }
else {
dimensionLabel.text = "Length" }
}
}
例子
文本标签,尺寸,不会更改为直径 when Circle is selected.
您的两个选择器视图都只有 1 个组件,即组件 0。
"Circle" 位于第 2 行(第 1 行),因此您的 if
语句需要检查 row == 1
,而不是 component == 1
.
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if pickerView.tag == 1 {
categoryOutput.text = category[row] }
if pickerView.tag == 2 {
shapeOutput.text = shape[row]
if row == 1 //Circle{
dimensionLabel.text = "Diameter" }
else {
dimensionLabel.text = "Length" }
}
}
我仔细搜索了多个页面和帖子,但无法完全找到我需要的东西。我目前正在处理一个项目,其中一个控制器有多个文本字段,输出使用单个 UIPickerView
,有时标签需要根据用户的选择进行更改。
如何根据 UIPickerView
的选定行更改标签,尤其是在 if 语句中?
我做了一个简单的测试代码和例子来演示。
代码
class TestViewController: UIViewController, UITextFieldDelegate {
@IBOutlet weak var categoryOutput: UITextField!
@IBOutlet weak var shapeOutput: UITextField!
@IBOutlet weak var lengthDiaOutput: UITextField!
@IBOutlet weak var heightOutput: UITextField!
@IBOutlet weak var dimensionLabel: UILabel!
let category = ["Perimeter", "Area"]
let shape = ["Rectangle", "Circle", "Triangle"]
// MARK: - viewDidLoad()
override func viewDidLoad() {
super.viewDidLoad()
createCategoryPicker()
createShapePicker()
}
// MARK: - UIPickerViews
// Create a UIPickerView as an input text field for 'Category'.
func createCategoryPicker() {
let categoryPicker = UIPickerView()
categoryPicker.delegate = self
categoryPicker.tag = 1
categoryOutput.inputView = categoryPicker
categoryPicker.backgroundColor = UIColor(red: 238/255.0, green: 238/255.0, blue: 238/255.0, alpha: 1.0)
}
// Create a UIPickerView as an input text field for 'Shape'.
func createShapePicker() {
let shapePicker = UIPickerView()
shapePicker.delegate = self
shapePicker.tag = 2
shapeOutput.inputView = shapePicker
shapePicker.backgroundColor = UIColor(red: 238/255.0, green: 238/255.0, blue: 238/255.0, alpha: 1.0)
}
}
// MARK: - Extensions
// Create a UIPickerView.
extension TestViewController: UIPickerViewDelegate, UIPickerViewDataSource {
// Set number of lists.
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
// Set number of rows in list.
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
if pickerView.tag == 1 {
return category.count }
if pickerView.tag == 2 {
return shape.count }
return 0
}
// Set content of list.
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
if pickerView.tag == 1 {
return category[row] }
if pickerView.tag == 2 {
return shape[row] }
return nil
}
// Grab selection and change label.
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if pickerView.tag == 1 {
categoryOutput.text = category[row] }
if pickerView.tag == 2 {
shapeOutput.text = shape[row]
if component == 1 {
dimensionLabel.text = "Diameter" }
else {
dimensionLabel.text = "Length" }
}
}
// Set appearance of contents.
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() }
if pickerView.tag == 1 {
label.text = category[row] }
if pickerView.tag == 2 {
label.text = shape[row] }
label.font = UIFont(name: "Avenir", size: 17)
label.textColor = .black
label.textAlignment = .center
return label
}
}
这是需要重点关注的部分。
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if pickerView.tag == 1 {
categoryOutput.text = category[row] }
if pickerView.tag == 2 {
shapeOutput.text = shape[row]
if component == 1 {
dimensionLabel.text = "Diameter" }
else {
dimensionLabel.text = "Length" }
}
}
例子
文本标签,尺寸,不会更改为直径 when Circle is selected.
您的两个选择器视图都只有 1 个组件,即组件 0。
"Circle" 位于第 2 行(第 1 行),因此您的 if
语句需要检查 row == 1
,而不是 component == 1
.
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if pickerView.tag == 1 {
categoryOutput.text = category[row] }
if pickerView.tag == 2 {
shapeOutput.text = shape[row]
if row == 1 //Circle{
dimensionLabel.text = "Diameter" }
else {
dimensionLabel.text = "Length" }
}
}