How to Store Data from a Picker View in a Variable When a Button Is Pressed

How to store data from a picker view in a variable when a button is pressed?

Alright, after trying different things I finally came up with a solution to my problem. It might not be what the more experienced developers do but it worked for me just like I wanted. Here is what I did:

@IBAction func saveButtonPressed(sender: AnyObject) {

var chosen = pickerView.selectedRowInComponent(0)
var chosenString = arrayPopulatingThePickerView[chosen]

}

I only have one component in the pickerView i need therefore when the button is pressed I store the row number of the component 0 in a variable, then I store the string in another variable by making it equal to the array I used to populate my picker view. I retrieve the row by passing it the chosen variable.

Save data from selected picker view row

You need to implement didSelectRow:

func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)
{
let variable = colors[row]
}

clicking on a button, to make pickerview appear, then choice appears in label

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)

Get selected value of a picker view that is present in different view and use the string in some other view

Step 1: Take a NSString variable in your appDelegate.

Step 2: create instance of appDelegate in your both view were your want to set value and get value as following.

yourApplicationDelegate *appDelegate;

Step 3: init appDelgate in implementation file as following

appDelegate = [[UIApplication sharedApplication] delegate];

after this statement you can use the string variable from both view controller you declared in appDelegate of you application.

That's it. now you can assign value from first view and access it in second view.

Swift make user selection on picker view the title for a button

I have used your code and it's working fine for me.

@IBAction func btnActionTapped(_ sender: UIButton) {

let condition = ["Blue", "Black", "Green", "Orange", "Purple"]

DPPickerManager.shared.showPicker(title: "String Picker", selected: "Value 1", strings: condition) { (value, index, cancel) in
if !cancel {
if let value = value {
sender.setTitle(value, for: .normal)
print(value)
}
}
}
}

I just changed following code to get value instead of optional value.

if let value = value {
print(value)
}

I’m getting string value there and print fine in console.

Use Data from Pickerview to change image in ARKit

You are going to need to handle selection in the picker

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

let pickerValue = pickerData[row]
let image = UIImage(named: pickerValue)
// do something with the selected image
material.diffuse.contents = image
}

EDIT:

Because you are getting the image within a tap gesture, we'll need a default image and we'll need to keep a reference to the last selected image. So when this first loads, if a user hasn't manually selected an image it will default to the first in the list

var defaultImage: UIImage {
return UIImage(named: pickerData[0]) // just select first value
}

var selectedImage: UIImage?

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

let pickerValue = pickerData[row]
selectedImage = UIImage(named: pickerValue)
}

Then in your current tapped function, edit the line that sets the image:

@objc func tapped(gesture: UITapGestureRecognizer) {
// ... previous code
material.diffuse.contents = selectedImage ?? defaultImage
}


Related Topics



Leave a reply



Submit