Uploading data to Firebase with "IF" and UIPickerController - ios

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" !

Related

UIPickerView get selected value without datasource

What would be the best way to get the value from a PickerView without having a datasource? So i can not read values from i.e. an array.
Current values are just row numbers, that i need to read after user is done with picking.
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 3
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
if component == 0 {
return 300
} else {
return 10
}
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
if component == 0 {
return "\(row)."
} else {
return "\(row)"
}
}
You can use pickerView.selectedRow(inComponent:)
let firstValue = pickerView.selectedRow(inComponent: 0)
let secondValue = pickerView.selectedRow(inComponent: 1)
Both will just be an Int value representing the currently selected row index in it's respective component. And since your values are starting from 0, you can use the values directly.
You simply add this func:
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
print(row)
}
Just take care that row starets at 0

uipickerview index out if range

I'm trying to learn to code Swift and recently I faced a bug which is so embarrassing.
I have 2 textfield which both are connected to a picker view.
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
if EntCity.isEditing
{
return MainCities[row].name
}
return MainJ[row].name
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if EntState.isFirstResponder
{
EntState.text = MainJ[row].name
Global_Addresses.global_address.cityID_fromState = MainJ[row].id
GetCities()
}
if EntCity.isEditing
{
if (EntState.text?.isEmpty)!
{
DispatchQueue.main.async {
self.DisplayMessage(UserMessage: "ابتدا استان را انتخاب کنید")
}
}
else
{
EntCity.text = MainCities[row].name
Global_Addresses.global_address.smallCityID = MainCities[row].id
}
}
EntState.text = MainJ[row].name
Global_Addresses.global_address.cityID_fromState = MainJ[row].id
GetCities()
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return MainCities.count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return MainCities[row].name
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
EntCity.text = MainCities[row].name
Global_Addresses.global_address.smallCityID = MainCities[row].id
print("?????????????????????")
print(Global_Addresses.global_address.smallCityID)
}
}
the code works fine and both textfield can show the data in the picker view to choose. these textfield are connected, it means that
you have to choose one of the rows in textfield number 1 to get the data for textfield number 2 . when i choose the first data , and select the second textfield to choose the second one , it works fine untill i choose of those data which their row number is bigger than the first textfield
and as you can see i have declared that if it select first or second textfield to return different number for row!

PickerView data not proper on using multiple pickerview

I am using multiple pickerview on one viewcontroller. I am getting correct data in the respective pickerviews but the view of rows symmetry in the pickerview is not correct. The data view in 1st pickerview is correct but in the second pickerview the rows start from the bottom of the pickerview.
I have attached screenshots of both pickerviews.
1st pickerview
2nd pickerview
For distinguishing between the 2 pickers, I have used pickerview tags.
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
if pickerView.tag == 31{
return arr_statename.count
}else{
return arr_cityname.count
}
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
if pickerView.tag == 31{
return arr_statename[row]
}else{
return arr_cityname[row]
}
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if pickerView.tag == 31{
state.text = arr_statename[row]
}else{
city.text = arr_cityname[row]
}
}
Can anyone please help? Thanks.
Try this i hope might be helpful!!
Just add the observer in view did load
NSNotificationCenter.defaultCenter().addObserver(self, selector:#selector(self.updatePicker), name: UITextFieldTextDidBeginEditingNotification, object: nil)
and Reload the components
func updatePicker(){
self.pickerView.reloadAllComponents()
}
Have you considered using a single pickerView with 2 components instead of two separate pickerViews on the same viewController? If it fulfills the needs of your app, using a single multi-component pickerView provides a better UI and avoids the types of problems you are experiencing.

Swift - how to concatenate didSelectRow selections from UIPickerView

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)"
}

Save UIPickerView selection to NSUserDefaults swift

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.

Resources