如何在UIPickerView中使用所选值

时间:2015-04-01 18:11:45

标签: ios swift uipickerview

新手,尝试创建一个简单的应用程序,用户选择出生年份和相应的变量。 我希望@IBAction func从底部的UiPicker获取变量birthYear。我将UIPicker的数据源和委托设置为连接检查器面板中的viewcontroller。

class ViewController: UIViewController,UIPickerViewDelegate {
var years = ["1991", "1992", "1993", "1994", "1995", "1996", "1997", "1998", "1999"]
let nineOne = "24"
let nineTwo = "23"
let nineThree = "22"
let nineFour = "21"
let nineFive = "20"
let nineSix = "19"
let nineSeven = "18"
let zero = "17"

@IBAction func get(sender: AnyObject) {

    if birthYear <= 1991
    {
        println(nineOne)
    }
    else if birthYear == 1992
    {
        println(nineTwo)

    }else if birthYear == 1993{
        println(nineThree)

    }
    else if birthYear == 1994{
        println(nineFour)

    }
    else if birthYear == 1995{
        println(nineFive)

    }
    else if birthYear == 1996{
        println(nineSix)

    }
    else if birthYear == 1997{
        println(nineSeven)

    }

    // current year minus 17
    else if birthYear >= 1998 {
        println(zero)

    }
}

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {

    return 1
}

func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int{

    return years.count
}

func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String!{

    return years[row]
}

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

    var birthYear = years[row]
}

1 个答案:

答案 0 :(得分:0)

如果你不知道如何继续,这里有2个教程:

  1. 使用UILabel显示结果:http://makeapppie.com/tag/uipickerview-in-swift/
  2. 在UITextField中显示结果:http://asanhussain.blogspot.ca/2012/11/uipickerview-in-uiactionsheet-for-iphone.html
  3. 注意

    的实施
    func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
    }
    

    另外,对于UITextField实现,需要将UIPickerView分配给UITextField的inputView。

    =============================================== ======================

    这是为了您的后续行动。您的代码中需要注意3个小问题。

    首先,您应该将UIPickerView的IBOutlet连接到此View Controller。在您的代码中,您连接一个名为get的IBAction(我认为它作为一个函数名称不是很好),用于其他一些视图组件。

    其次,您没有将选择器视图的委托分配给当前视图控制器。因此,这些代表方法不会被调用。

    第三,birthYear的类型是String。它无法直接与Int进行比较。

    如果您将故事板中的PickerView连接到此IBOutlet,以下是修改后的代码。希望它有所帮助。

    import UIKit
    
    class ViewController: UIViewController,UIPickerViewDelegate {
    
    
    @IBOutlet var pickerView: UIPickerView!
    
    var years = ["1991", "1992", "1993", "1994", "1995", "1996", "1997", "1998", "1999"]
    let nineOne = "24"
    let nineTwo = "23"
    let nineThree = "22"
    let nineFour = "21"
    let nineFive = "20"
    let nineSix = "19"
    let nineSeven = "18"
    let zero = "17"
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    
        pickerView.delegate = self
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
    
        return 1
    }
    
    func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int{
    
        return years.count
    }
    
    func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String!{
    
        return years[row]
    }
    
    func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
    
        var birthYear = years[row]
    
        self.printYear(birthYear)
    }
    
    func printYear(birthYearString: String) {
    
        if let birthYear = birthYearString.toInt() {
            if birthYear <= 1991
            {
                println(nineOne)
            }
            else if birthYear == 1992
            {
                println(nineTwo)
    
            }else if birthYear == 1993{
                println(nineThree)
    
            }
            else if birthYear == 1994{
                println(nineFour)
    
            }
            else if birthYear == 1995{
                println(nineFive)
    
            }
            else if birthYear == 1996{
                println(nineSix)
    
            }
            else if birthYear == 1997{
                println(nineSeven)
    
            }
    
                // current year minus 17
            else if birthYear >= 1998 {
                println(zero)
    
            }
    
        }
    }
    }