2017-06-02 162 views
0

我有我的UIPickerView麻煩選擇正確的值。我的代碼當前採用由選取器選擇的行並將其存儲在Firebase中。不過,我注意到,記錄到Firebase中的值與我的應用模擬器中選擇器視圖中選擇的值不同。更具體地說,該值將始終是我在選擇器視圖中預期的幾行以上或以下的行。我的代碼如下:火力地堡不存儲在UIPickerView

import UIKit 
import Firebase 
import FirebaseAuth 

class DormViewController: UIViewController, UIPickerViewDelegate,  UIPickerViewDataSource { 

@IBOutlet weak var dormName: UIPickerView! 
@IBOutlet weak var entryway: UIPickerView! 
@IBOutlet weak var roomNumber: UIPickerView! 
@IBOutlet weak var nextButton: UIButton! 

var ref: DatabaseReference! 

var dorm = "" 
var entry = "" 
var num = "" 

let dorms = ["Adams", "Apley", "Cabot", "Canaday", "Currier", "DeWolfe 10", "Dewolfe 20", "Dunster", "Eliot", "Fairfax", "Grays", "Greenough", "Hollis", "Holworthy", "Hurlbut", "Inn at Harvard", "Kirkland", "Leverett Towers", "Leverett McKinlock Hall", "Lionel", "Lowell", "Massachusetts Hall", "Mather Lowrise", "Mather Tower", "Matthews", "Mower", "New Quincy", "Pennypacker", "Pforzheimer", "Pforzheimer Jordan North", "Pforzheimer Jordan South", "Prescott", "Stone Hall", "Stoughton", "Straus", "Thayer", "Weld", "Wigglesworth", "Winthrop" ] 

let entrances = ["N/A", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"] 

let rooms = ["N/A", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "40", "41", "42", "43", "44", "45", "46", "47", "48", "49", "50", "51", "52", "53", "54", "55", "56", "57", "58", "59", "60", "61", "62", "63", "64", "65", "66", "67", "68", "69", "70", "71", "72", "73", "74", "75", "76", "77", "78", "79", "80", "81", "82", "83", "84", "85", "86", "87", "88", "89", "90", "91", "92", "93", "94", "95", "96", "97", "98", "99"] 

override func viewDidLoad() { 
    super.viewDidLoad() 

    ref = Database.database().reference() 

    dormName.tag = 0 
    entryway.tag = 1 
    roomNumber.tag = 2 

    // Do any additional setup after loading the view. 
} 

func numberOfComponents(in pickerView: UIPickerView) -> Int { 
    return 1 
} 

func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? { 
    if pickerView.tag == 0 { 
     dorm = dorms[row] 
     return dorm 
    } 
    else if pickerView.tag == 1 { 
     entry = entrances[row] 
     return entry 
    } 
    else { 
     num = rooms[row] 
     return num 
    } 
} 

func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int { 
    if pickerView.tag == 0 { 
     return dorms.count 
    } 
    else if pickerView.tag == 1 { 
     return entrances.count 
    } 
    else { 
     return rooms.count 
    } 
} 


@IBAction func nextButton(_ sender: Any) { 
    let user = Auth.auth().currentUser 
    if user != nil { 
     self.ref.child("Users").child((user?.uid)!).updateChildValues(["Location": dorm + " " + entry + "-" + num]) 
     performSegue(withIdentifier: "tohomepage", sender: self) 
    } 
    else { 
     print("error") 
    } 
} 

}

例如,如果我打算選擇亞當斯A-3,火力地堡可以登記自己選擇作爲卡博特d-7。這是我的故事板的澄清截圖:

[![截圖] [1] [1]

我的火力地堡的截圖:

enter image description here

請讓我知道爲什麼我的選擇器視圖沒有註冊我選擇的值。謝謝!

+0

道歉重複截圖,這是不打算 –

回答

1

我想你已經錯過了選擇的值存儲選擇器的委託方法。

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, forComponent component: Int) -> String? { 
    if pickerView.tag == 0 { 
     dorm = dorms[row] 
    } 
    else if pickerView.tag == 1 { 
     entry = entrances[row] 
    } 
    else { 
     num = rooms[row] 
    } 
}