xcode 在 UIPickerView 中选择一行 - swift

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37376703/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 08:49:50  来源:igfitidea点击:

Selecting a row in a UIPickerView - swift

xcodeswiftuipickerview

提问by Niall Kehoe

I'm trying to select a row of a picker view in Swift.I know there are other questions like this but they all have the same answers which doesn't work for me. Such questions can be located at : UIPickerView selectRow doesn't works.

我正在尝试在 Swift 中选择一行选择器视图。我知道还有其他类似的问题,但它们都有相同的答案,但对我不起作用。此类问题可以位于:UIPickerView selectRow 不起作用

They all point to use this code in the viewDidAppear:

他们都指出在以下代码中使用此代码viewDidAppear

 pickerview.selectRow(3, inComponent: 0, animated: true)

But the View Controller still looks like this: simulator

但是视图控制器仍然是这样的: 模拟器

Any ideas on how to select a row?

关于如何选择一行的任何想法?

采纳答案by DHShah01

It looks like you only have 3 elements in your picker. Pickerview element rows are 0-indexed, so instead of

看起来您的选择器中只有 3 个元素。Pickerview 元素行是 0 索引的,所以不是

pickerview.selectRow(1, inComponent: 0, animated: true)
pickerview.selectRow(2, inComponent: 0, animated: true)
pickerview.selectRow(3, inComponent: 0, animated: true)

you'll want

你会想要

pickerview.selectRow(0, inComponent: 0, animated: true)
pickerview.selectRow(1, inComponent: 0, animated: true)
pickerview.selectRow(2, inComponent: 0, animated: true)