change initial row pickerView

时间:2015-09-01 22:14:06

标签: ios swift uipickerview

I read here that you have to use pickerView.selectRow inside viewDidAppear(). While pickerView.selectRow is working inside func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) it does not work in viewDidAppear().

I just want to set the initial row to another row than 0

What am I missing here?

import UIKit

class FirstViewController: UIViewController, UIPickerViewDelegate {

    let scrollerOfset = 100
    var kiloMeters = [Int](-100...200)

    @IBOutlet weak var pickerActive: UILabel!

    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.
    }

    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        pickerView.selectRow(scrollerOfset, inComponent: 0, animated: false)
    }

}

Xcode gives this error

Cannot invoke 'selectRow' with an argument list of type '(Int, inComponent: Int, animated: Bool)' at line 32 (in viewDidAppear(animated: Bool))

Update

Code changed to this to make it work:

import UIKit

class FirstViewController: UIViewController, UIPickerViewDelegate {

    let scrollerOfset = 100
    var kiloMeters = [Int](-100...200)

    @IBOutlet weak var pickerActive: UILabel!
    @IBOutlet weak var picker: UIPickerView!

    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.
    }

    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        picker.selectRow(scrollerOfset, inComponent: 0, animated: false)
    }

}

1 个答案:

答案 0 :(得分:1)

您似乎错过了班级中pickerView属性的声明,请尝试在pickerActive下声明,例如

  @IBOutlet weak var pickerActive: UILabel!
  @IBOutlet weak var picker: UIPickerView!

然后通过IB连接它,如pickerActive