0

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]
        }
    }
}
maxwell
  • 3,788
  • 6
  • 26
  • 40
mrpotter
  • 17
  • 1
  • 8

2 Answers2

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
}
Durdu
  • 4,649
  • 2
  • 27
  • 47
  • 3
    where do put that in my code? and what do you mean who corresponds to the sender exactly? thanks for the help! – mrpotter Feb 02 '18 at 16:00
0

I think what your looking for is something similar to the following:

Update:

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]
        }
    }
}

Note: I did this in playground so its not properly tested but it should point you in the right direction (hopefully)

kd02
  • 422
  • 5
  • 14
  • love u youre amazing – mrpotter Feb 02 '18 at 17:25
  • wait so the next button is for the next screen, like not next button to the next form – mrpotter Feb 02 '18 at 17:47
  • Sorry didn’t factor in your next button. In my mind the next button would take you to the next screen unless I’m misunderstanding the intended functionality? – kd02 Feb 02 '18 at 19:45
  • no worries at all, thanks again for the help! my next button takes you to the next screen, i am looking at this screen with the 5 buttons as step 1 and the next button takes you to step 2 – mrpotter Feb 02 '18 at 20:04
  • can we chat i have a couple questions about this, nothing major, how do i start a pm here? – mrpotter Feb 05 '18 at 15:02
  • @mrpotter, unfortunately stackoverflow doesn't do direct messaging but feel free to put you question here (as a comment) and I'll either update my answer or just reply as a comment – kd02 Feb 05 '18 at 15:24
  • i readded my code mate, can you continue to be amazing and help please on how to add those other views please? you rule my world. thank u if u dont mind helping – mrpotter Feb 06 '18 at 14:55
  • @mrpotter this should be now slightly more refined and closer to what you need. Let me know if you have any issues. – kd02 Feb 07 '18 at 12:01