I am trying to save the user's selection from UIPickerView to NSUserDefaults. I have found answers in obj-c but not swift. Here is my UIPickerView code:
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return pickerData.count
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {
return pickerData[row]
}
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
race = pickerData[row]
}
And when you press a save button, it is supposed to save what is selected to NSUserDefaults. Here is my saving code:
#IBAction func save(sender: AnyObject) {
var defaults: NSUserDefaults = NSUserDefaults.standardUserDefaults()
defaults.setObject(race, forKey: "race")
}
When I try to display the text on a label, it shows up blank:
override func viewDidLoad() {
super.viewDidLoad()
self.raceLabel.text = defaults.objectForKey("race") as? String
Let me know if my question needs any clarification, thanks.
Update your code as specified below and it will work
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
race = row
}
Every time you select a row, this function is called and the argument of the parameter row represents the row that was selected in your UIPickerView.
If you assign pickerData[row] value to race, you are assigning a value from your pickerData array at the index number represented by the row selected in your UIPickerView Object.
Related
so I have a UIPicker view. Now I have something selected now with that I want to save that selection so that the next time I go back to that page the same option that I selected is still there. right now it just goes to the first option selected which is grams.
Thx I'm new to Swift
Here is my code
#IBOutlet weak var pickerView: UIPickerView!
var measurementTypes = ["Grams","Ounces", "Pounds"]
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return measurementTypes.count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return measurementTypes[row]
}
// Catpure the picker view selection
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
selected = measurementTypes[row]
// This method is triggered whenever the user makes a change to the picker selection.
// The parameter named row and component represents what was selected.
}
You can simply save the selected row number in UserDefaults so that it can be accessed next time.
// Capture the picker view selection
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
selected = measurementTypes[row]
// Save selected row integer in User Defaults
UserDefaults.standard.set(row, forKey: "pickerViewRow")
}
Then, in override func viewDidLoad() you can retrieve this value and set the picker view:
let row = UserDefaults.standard.integer(forKey: "pickerViewRow")
pickerView.selectRow(row, inComponent: 0, animated: False)
I have two pickerView and two Label on my ViewController.
The question is how I could connect labels to pickerView's data array (string type) and show in labels chosen pickerView cell?
#IBOutlet weak var valuetype1: UILabel!
var pickerData: [String] = [String]()
override func viewDidLoad() {
super.viewDidLoad()
self.picker1.delegate = self
self.picker1.dataSource = self
self.picker2.delegate = self
self.picker2.dataSource = self
pickerData = ["Годы", "Месяцы", "Недели", "Дни", "Часы", "Минуты"]
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return pickerData.count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return pickerData[row]
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
valuetype1.text = pickerData[row]
valuetype2.text = pickerData[row]
}
How could you see I connected labels (valuetype(1..2)) to pickerData but it always show in both labels one String. I need to show in first label String chosen in pickerView1 and another string in second label, chosen in pickerView2.
UIPickerViewDelegate has a method called pickerView(_:didSelectRow:inComponent:). Add this method to your VC class. This method gets called whenever the user selects a row.
Your implementation should look somewhat similar to this.
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
valuetype1.text = pickerData[row]
}
For more information refer to the apple docs: https://developer.apple.com/documentation/uikit/uipickerviewdelegate/1614371-pickerview
EDIT:
When you are using the same controller as a delegate for multiple UIPickerViews, then you can check which picker view triggered the method call simply by comparing the pickerView argument on the pickerView(_:didSelectRow:inComponent:) method.
Try this:
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if pickerView == picker1 {
valuetype1.text = pickerData[row]
} else if pickerView == picker2 {
valuetype2.text = pickerData[row]
}
}
I have a two column picker (Miles and Tenths)
Picker seems to work well, but I cannot seem to work out how to concatenate the selections from the two columns i.e. if user selects 23 from first column and 6 from second column, I want to create an answer of "23.6"
At the moment, either column selection overwrites the previous selection.
BTW: I don't think I can append two strings because what happens if user selects the 10ths first?
let resetPickerData = [Array(0...99), Array(0...9)]
// returns the number of 'columns' to display.
func numberOfComponentsInPickerView(pickerView: UIPickerView!) -> Int{
return 2
}
// returns the # of rows in each component..
func pickerView(pickerView: UIPickerView!, numberOfRowsInComponent component: Int) -> Int{
return resetPickerData[component].count
}
func pickerView(pickerView: UIPickerView!, titleForRow row: Int, forComponent component: Int) -> String! {
return String(resetPickerData[component][row])
}
func pickerView(pickerView: UIPickerView!, didSelectRow row: Int, inComponent component: Int)
{
//var distance = ??
}
You'll need to keep track of the component selection separately like this then concant the strings:
var firstComponentString = ""
var secondCompoentString = ""
func pickerView(pickerView: UIPickerView!, didSelectRow row: Int, inComponent component: Int)
{
if component == 0 {
firstComponentString = String(resetPickerData[component][row])
}
else if component == 1 {
secondCompoentString = String(resetPickerData[component][row])
}
var displayString = "\(firstComponentString).\(secondCompoentString)"
}
I started coding a few weeks ago and I am trying to load data to my Firebase.database for my app. But the thing is, that I want the data to load/save into different folders/names ("A" & "B") in my database in firebase so that later i can retrieve the data on 2 different scroll views with one scroll view only showing "A" and one only showing "B".
My code is not throwing errors at me, but for some reason it won't upload data. i don't know why it is not working.
Help highly appreciated.
Thanks community !!
Code incoming !
/// Picker now
// 2 items for the picker.
var data = ["A", "B"]
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
// Column count: use one column.
return 1
}
func pickerView(pickerView: UIPickerView,
numberOfRowsInComponent component: Int) -> Int {
// Row count: rows equals array length.
return data.count
}
func pickerView(pickerView: UIPickerView,
titleForRow row: Int,
forComponent component: Int) -> String? {
// Return a string from the array for this row.
return data[row]
}
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
true
}
//Here I try to Upload the image that I picked in UIPickerView
#IBAction func Upload(sender: UIButton){
func simplePicker(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if component == 0 {
func Upload(){
_ = UIImage.self
FIRDatabase.database().reference().childByAutoId().setValue("A")
}}
if component == 1 {
func Upload(){
_ = UIImage.self
FIRDatabase.database().reference().childByAutoId().setValue("B")
}}
}
}
Try:-
var valueToStore : String = "Placeholder"
#IBAction func Upload(sender: UIButton){
FIRDatabase.database().reference().childByAutoId().setValue(valueToStore)
//Storing the values the value you want to store ..
}
func simplePicker(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if component == 0 {
valueToStore = "A"
}else if component == 1 {
valueToStore = "B"}
//Setting the value you want to store in a global variable..
}
finally I managed to find a way. I will Post the solution that worked for me. Maybe it will help someone in the future.
Cheers, Leo
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if row == 0 {FIRDatabase.database().reference().childByAutoId().setValue("A")}
if row == 1 {FIRDatabase.database().reference().childByAutoId().setValue("B")}
}
P.s. The Problem was the phrasing. It is not components. It is "rows" !
I want to know how to add interval value into UIDateTimePicker in swift
8am-8:30am
8:30am-9am
9am-9:30am
9:30am-10am
10am-10:30am
10:30am-11am
You can try something like this. Add one UIPickerView in your xib or your storyboard viewController after that set the delegate and datasoucre of that UIPickerView with your viewController. Now create one array of string like this.
var timeArr: [String] = [String]()
Now initialize this array in viewDidLoad like this
override func viewDidLoad() {
super.viewDidLoad()
self.timeArr = ["8:00AM - 8:30AM", "8:30AM - 9:00AM", "9:00AM - 9:30AM", "9:30AM - 10:00AM"]
}
Now add the delegate and datasource method of UIPicker in your ViewController like this
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return self.timeArr.count
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return self.timeArr[row]
}
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
print(self.timeArr[row])
}
This will create Picker that you want to select time.