gpt4 book ai didi

swift - PickerView 的 selectRow 如何影响 Button 的启用?

转载 作者:行者123 更新时间:2023-11-30 10:43:07 24 4
gpt4 key购买 nike

我有 UIPickerView 和按钮。我希望如果我选择原始数字 0,则按钮不会启用,反之亦然。我的代码中有这样的结构:

    import UIKit

class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {
...
@IBOutlet weak var buttonOutlet: UIButton!
@IBOutlet weak var firstPicker: UIPickerView!
@IBOutlet weak var secondPicker: UIPickerView!

let firstData = ["00", "01", "02", "03", "04", "05", "06", "07", "08", "09", "10"]
let secondData = ["00", "10", "20", "30", "40", "50"]

@IBOutlet weak var firstLabel: UILabel!
@IBOutlet weak var secondLabel: UILabel!

var first: Int!
var second: Int!

var firstRaw = 1
var secondRaw = 0
...
override func viewDidLoad() {
super.viewDidLoad()

self.firstPicker.delegate = self
self.firstPicker.dataSource = self
self.secondPicker.delegate = self
self.secondPicker.dataSource = self

firstPicker.selectRow(firstRaw, inComponent: 0, animated: true)
secondPicker.selectRow(firstRaw, inComponent: 0, animated: true)
if (self.firstRaw == 0) && (self.secondRaw == 0) {
self.buttonOutlet.isEnabled = false
} else {
self.buttonOutlet.isEnabled = true
}
...
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if pickerView == firstPicker {
...
} else {
...
}

为什么这个结构不起作用?

最佳答案

要检测何时选择某个值,您必须实现 UIPickerViewDelegate,同时实现选取器 View 的 didSelectRow 方法。我假设您已经填充了选择器 View 数据。

class ViewController : UIViewController, UIPickerViewDelegate {

@IBOutlet weak var buttonOutlet: UIButton!
@IBOutlet weak var firstPicker: UIPickerView!
var firstRaw = 1
...
override func viewDidLoad() {
super.viewDidLoad()
self.firstPicker.delegate = self
...
}
func pickerView(pickerView: UIPickerView!, didSelectRow row: Int, inComponent component: Int){
if self.firstRaw == 0 {
self.buttonOutlet.isEnabled = false
} else {
self.buttonOutlet.isEnabled = true
}
}
}

关于swift - PickerView 的 selectRow 如何影响 Button 的启用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56377771/

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