0

Here is the code that I got help on previously on here, I just cannot quite tell what is missing for my code to work. I have it set up so a button is pressed to make the pickerview appear and then the choice appears in a label. But when I click on the buttons it does not make the pickerview appear. I guess from what I can tell, the issue is that I am missing a connection that would trigger one to make the other appear, my pickerview is set to hidden and also it appears over top a stackview when it should pop up.

    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]
    }
}
}
mrpotter
  • 17
  • 1
  • 8
  • Could it just be that your text field isn't first responder? Having a bit of trouble imagining this user interface. – Ash Feb 09 '18 at 15:37
  • yeah sorry i didnt explain it well. so screen is in two parts. left side is the buttons you push, right side are the labels, but when the button is pushed, the pickerview i am hoping covers the right side where the labels are, but once the choice is made it shows up in that label, also the left and right side are in stacked views for the buttons and labels, thanks for any help – mrpotter Feb 09 '18 at 15:42

1 Answers1

0

It looks like you never set the correct text field as the first responder.

BTW, all your long if/else if/else if... constructs would be cleaner as switch statements, or you could even set up dictionaries with the buttons as the key and then a struct containing the data source and the picker view for that button:

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

And then code like your button action could use the dictionary:

@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() 
}
Duncan C
  • 128,072
  • 22
  • 173
  • 272
  • Not tried this but, from memory, wouldn't this cause the picker to appear in place of the standard keyboard, rather than in the place it was designed? – Ash Feb 09 '18 at 15:50
  • Yes it would. I think that's what the OP wants. – Duncan C Feb 09 '18 at 15:50
  • I thought so too at first - see the comments on the main question above though. I think he wants it to appear over the top of the right-hand side of the screen. – Ash Feb 09 '18 at 15:51
  • If that's NOT what the OP wants then he won't be able to use the textField's inputView, and will need to write custom code to display a picker, collect the user's selection, and install the user's choice into display-only fields. – Duncan C Feb 09 '18 at 15:52
  • wiat duncan if i update my code can u tell me if i put the stuff in the right places please? – mrpotter Feb 09 '18 at 21:29
  • I can try. If I don't get to it chances are somebody else can offer feedback. – Duncan C Feb 09 '18 at 21:53