我已经将我的viewcontroller设置为具有5个标签和一个pickerview的5个按钮。我希望设置它,所以在点击每个按钮时,它将根据选择的按钮拉出选择器视图,然后选择的选项将显示在按钮旁边的标签中。
我根据试图解决这个问题所做的事情是我的swift文件上有io个插座连接的所有按钮和标签,然后我真的不知道在那之后还能做什么。
我已经查看是否有其他指南连接按钮+ pickerview +标签的所有三个变量,但没有使用该按钮作为触发器。
shape
答案 0 :(得分:0)
bBTN.addTarget(self, action: #selector(doSomething:), for: .touchUpInside)
// also the other buttons
@objc func doSomething(_ sender:UIButton!) {
// check who coresponds to the sender and update the label you want
}
答案 1 :(得分:0)
我认为你所寻找的东西类似于以下内容:
<强>更新强>
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 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]
}
}
}
注意:我在操场上做了这个,所以没有经过适当的测试,但它应该指向正确的方向(希望如此)