我读过here,您必须在pickerView.selectRow中使用viewDidAppear()。虽然pickerView.selectRowfunc pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)中运行,但在viewDidAppear()中不起作用。

我只想将初始行设置为非0的另一行

我在这里想念什么?

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给出此错误

无法使用类型为((Int,inComponent:Int,animation:Bool)'的参数列表调用'selectRow'
在第32行(在viewDidAppear(animated: Bool)中)

更新资料

代码已更改为使其正常工作:
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)
    }

}

最佳答案

似乎您在类中缺少pickerView属性的声明,请尝试在pickerActive下进行声明,例如

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

然后像pickerActive一样通过IB连接

关于ios - 更改初始行pickerView,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32341698/

10-12 02:59