I have set up my viewcontroller to have a 5 buttons with 5 labels and one pickerview. I am hoping to have it set up so upon each button clicked it will pull up the pickerview according to which button is chosen and then the choice that is chosen will show up in the label next to the button.
What I have done in accordance to trying to solve this is I have all the buttons and labels hooked up by io outlets on my swift file, then I really don't know what else to do after that.
I have looked into if there are other guides that connect all three variables of a button + pickerview + label, but none use the button as a trigger.
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 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 lastPressedButton: UIButton
override func viewDidLoad() {
super.viewDidLoad()
firstButton.addTarget(self, action:#selector(self.buttonClicked(:)), forControlEvents: .touchUpInside)
secondButton.addTarget(self, action:#selector(self.buttonClicked(:)), forControlEvents: .touchUpInside)
thirdButton.addTarget(self, action:#selector(self.buttonClicked(:)), forControlEvents: .touchUpInside)
fourthButton.addTarget(self, action:#selector(self.buttonClicked(:)), forControlEvents: .touchUpInside)
fifthButton.addTarget(self, action:#selector(self.buttonClicked(:)), forControlEvents: .touchUpInside)
self.pickerView.dataSource = self;
self.pickerView.delegate = self;
}
func buttonAction(sender:UIButton!) {
lastPressedButton = sender
if lastPressedButton == firstButton {
firstTextField.inputView = pickerView
} else if lastPressedButton == secondButton {
secondTextField.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
}
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {
if lastPressedButton == firstButton {
return firstButtonDataSource[row]
} else if lastPressedButton == secondButton {
return secondButtonDataSource[row]
}
}
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if lastPressedButton == first {
self.firstTextField.text = firstButtonDataSource[row]
} else if lastPressedButton == secondButton {
self.secondTextField.text = firstButtonDataSource[row]
}
}
}