gpt4 book ai didi

ios - 为什么我的按钮不让我的选择器 View 出现?

转载 作者:行者123 更新时间:2023-11-30 11:47:06 30 4
gpt4 key购买 nike

这是我之前在这里获得帮助的代码,我只是不太清楚我的代码缺少什么功能。我已将其设置为按下按钮以使选择器 View 出现,然后选择出现在标签中。但是当我单击按钮时,它不会出现选择器 View 。据我所知,问题是我缺少一个连接,该连接会触发一个连接使另一个出现,我的选择器 View 设置为隐藏,并且当它应该弹出时它会出现在堆栈 View 之上。

    import UIKit

class CalculatorViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate {

@IBOutlet weak var firstButton: UIButton!
@IBOutlet weak var secondButton: UIButton!
@IBOutlet weak var thirdButton: UIButton!
@IBOutlet weak var fourthButton: UIButton!
@IBOutlet weak var fifthButton: UIButton!
@IBOutlet weak var nextPageButton: UIButton!

@IBOutlet weak var firstTextField: UITextField!
@IBOutlet weak var secondTextField: UITextField!
@IBOutlet weak var thirdTextField: UITextField!
@IBOutlet weak var fourthTextField: UITextField!
@IBOutlet weak var fifthTextField: UITextField!

@IBOutlet weak var pickerView: UIPickerView!

var firstButtonDataSource = ["1", "2", "3", "4"];
var secondButtonDataSource = ["White", "Red", "Green", "Blue"];
var thirdButtonDataSource = ["Mike", "Steve", "Ben", "Peter"];
var fourthButtonDataSource = ["Large", "Medium", "Small", "Extra-small"];
var fithButtonDataSource = ["USA", "UK", "France", "Germany"];

var lastPressedButton: UIButton?

override func viewDidLoad() {
super.viewDidLoad()

firstButton.addTarget(self, action:#selector(buttonClicked(sender:)), for: .touchUpInside)
secondButton.addTarget(self, action:#selector(buttonClicked(sender:)), for: .touchUpInside)
thirdButton.addTarget(self, action:#selector(buttonClicked(sender:)), for: .touchUpInside)
fourthButton.addTarget(self, action:#selector(buttonClicked(sender:)), for: .touchUpInside)
fifthButton.addTarget(self, action:#selector(buttonClicked(sender:)), for: .touchUpInside)

self.pickerView.dataSource = self;
self.pickerView.delegate = self;
}

@objc func buttonClicked(sender:UIButton!) {

lastPressedButton = sender

if lastPressedButton == firstButton {
firstTextField.inputView = pickerView
} else if lastPressedButton == secondButton {
secondTextField.inputView = pickerView
} else if lastPressedButton == thirdButton {
thirdTextField.inputView = pickerView
} else if lastPressedButton == fourthButton {
fourthTextField.inputView = pickerView
} else if lastPressedButton == fifthButton {
fifthTextField.inputView = pickerView
}
}

func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}

func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {

if lastPressedButton == firstButton {
return firstButtonDataSource.count;
} else if lastPressedButton == secondButton {
return secondButtonDataSource.count;
} else if lastPressedButton == thirdButton {
return thirdButtonDataSource.count;
} else if lastPressedButton == fourthButton {
return fourthButtonDataSource.count;
} else if lastPressedButton == fifthButton {
return fithButtonDataSource.count;
}

return 0
}

func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {

if lastPressedButton == firstButton {
return firstButtonDataSource[row];
} else if lastPressedButton == secondButton {
return secondButtonDataSource[row];
} else if lastPressedButton == thirdButton {
return thirdButtonDataSource[row];
} else if lastPressedButton == fourthButton {
return fourthButtonDataSource[row];
} else if lastPressedButton == fifthButton {
return fithButtonDataSource[row];
}

return ""
}

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

if lastPressedButton == firstButton {
self.firstTextField.text = firstButtonDataSource[row]
} else if lastPressedButton == secondButton {
self.secondTextField.text = secondButtonDataSource[row]
} else if lastPressedButton == thirdButton {
self.thirdTextField.text = thirdButtonDataSource[row]
} else if lastPressedButton == fourthButton {
self.fourthTextField.text = fourthButtonDataSource[row]
} else if lastPressedButton == fifthButton {
self.fifthTextField.text = fithButtonDataSource[row]
}
}
}

最佳答案

您似乎从未将正确的文本字段设置为第一响应者。

顺便说一句,所有长 if/else if/else if... 构造都会像 switch 语句一样干净,或者您可以甚至设置以按钮为键的字典,然后设置一个包含数据源和该按钮的选择器 View 的结构:

struct ButtonData {
let buttonDataSource: [String]
let buttonTextField: UITextField
}

let buttonsDict = [
firstButton: ButtonData(buttonDataSource: ["1", "2", "3", "4"],
buttonTextField: firstTextField),
secondButton: ButtonData(buttonDataSource: ["White", "Red", "Green", "Blue"],
buttonTextField: secondTextField),
thirdButton: ButtonData(buttonDataSource: ["Mike", "Steve", "Ben", "Peter"],
buttonTextField: thirdTextField),
fourthButton: ButtonData(buttonDataSource: ["Large", "Medium", "Small", "Extra-small"],
buttonTextField: fourthTextField),
fifthButton: ButtonData(buttonDataSource: ["USA", "UK", "France", "Germany"],
buttonTextField: fifthTextField)
]

然后像按钮操作这样的代码可以使用字典:

@objc func buttonClicked(sender:UIButton!) {

lastPressedButton = sender

guard let buttonStruct = buttonsDict[lastPressedButton] else { return }

let textField = buttonsStruct.buttonTextField

textField.inputView = pickerView

//I think this the code you need to show the picker
textField.becomeFirstResponder()
}

关于ios - 为什么我的按钮不让我的选择器 View 出现?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48709087/

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