如何在swift中使用多个滚动条从ui选取器视图中获取数据

时间:2016-04-10 20:58:55

标签: ios swift firebase uipickerview

我试图使用一个具有3个滚动选项的ui选择器视图,但我不能吐出我按下提交按钮时选择的选项,但它只是给1,2,3这里是我的代码代码(也不要介意它的不完整警报信息

import UIKit
import Firebase

class PitcherPickerViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {

var ref: Firebase! @IBOutlet weak var picker: UIPickerView! @IBOutlet weak var age: UILabel! @IBOutlet weak var paragraph: UILabel! @IBOutlet weak var playerNum: UILabel! @IBOutlet weak var innings: UILabel! @IBOutlet weak var topBlue: UILabel! @IBOutlet weak var back: UIButton! @IBAction func submit1(sender: AnyObject) { ref.setValue(pickerData) } var pickerData: [[String]] = [[String]]() override func viewDidLoad() { super.viewDidLoad() ref = Firebase(url: "https://baseball-pitcher-app.firebaseIO.com/") // Do any additional setup after loading the view. // Connect data: self.picker.delegate = self self.picker.dataSource = self // Input data into the Array: pickerData = [["8", "9", "10"], ["0", "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", "91", "92", "93", "94", "95", "96", "97", "98", "99"], ["1", "2", "3", "4", "5", "6"]] } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } // The number of columns of data func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int { return 3 } // The number of rows of data func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int { return pickerData[component].count } // The data to return for the row and component (column) that's being passed in func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? { return pickerData[component][row] } // Catpure the picker view selection func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) { // This method is triggered whenever the user makes a change to the picker selection. // The parameter named row and component represents what was selected. } func displayMyAlertMessage(userMessage:String) { var myAlert = UIAlertController(title: "Oh No! ", message: userMessage, preferredStyle: UIAlertControllerStyle.Alert) let okAction = UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: nil) myAlert.addAction(okAction) self.presentViewController(myAlert, animated: true, completion: nil) }

}

这是卷轴的图片 ui picker image

我已经被困了差不多一个星期了,感谢你的帮助,我似乎无法想象我的生活。

1 个答案:

答案 0 :(得分:0)

如果你想知道3个不同组件中的选定项目,如果你编写一个方法可以分别调用选择器视图selectedRowInComponent 3个,那么它可能是最容易的,每个组件一个(传递组件值0,1和2.)

编辑:

我建议将该逻辑放在IBAction的提交按钮中。

您还可以在didSelectRow:inComponent方法中放置逻辑,该方法每次更改时都会保存所选组件,但这对于初学者来说更有效,可能会更加混乱。