UIPickerView - 第一行选择不调用 didSelectRow(在 Swift 中)
UIPickerView - 1st row selection does not call didSelectRow (in Swift)
我有一个 UIPickerView
,当用户 select 一行并按下名为 'Start' 的按钮时,该行 selected 将启动一个特定的方法。
要检测选择器中的 selected 行(并更新变量),我使用以下代码:
var picked = 15
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if row == 0 {
picked = 1
}
else if row == 1 {
picked = 2
}
else if row == 2 {
picked = 3
}
}
只要用户对选择器进行更改 selection,就会触发此方法。但是,如果用户没有 select 选择器上的任何值(因为用户想要 select 已经 selected 的第一个选项),则不会调用此方法。
我知道关于这个问题已经有一个很好回答的问题,但这是 Objective C (uipickerview-1st-row-selection-does-not-call-didselectrow)。我想要 Swift 的答案,因为我没有 OBjective C 技能。
所以我的问题是:如何让已经 selected 的选择器的第一个选项被用户检测为 selected 行? (所以这个已经-selected-行将更新变量)
我在viewDidAppear
中添加了代码:
picker1.selectRow(2, inComponent: 0, animated: true)
// scrolls the specified row to center.
但这只显示指定的行,而不是 RETURNS didSelectRow 函数的指定行。所以这不适合我
文档说(在 https://developer.apple.com/library/prerelease/ios/documentation/UIKit/Reference/UIPickerViewDelegate_Protocol/index.html#//apple_ref/occ/intfm/UIPickerViewDelegate/pickerView:didSelectRow:inComponent:):"Called by the picker view when the user selects a row in a component."。因此,您应该通过 - (NSInteger)selectedRowInComponent:(NSInteger)component / func selectedRowInComponent(_ component: Int) -> Int 查询选择器本身的状态。
解决此问题的一种简单易用的方法是将每个选择器中的零行设为用户邀请,而不是已处理的值。您可以在零行中放置 'Select' 或“---”之类的内容。这样,用户将被迫与选择器交互,而不会依赖被动地坐在那里的默认值。这在多组件选择器中特别有用,用户可能会更改一些但不是全部组件。
我有一个 UIPickerView
,当用户 select 一行并按下名为 'Start' 的按钮时,该行 selected 将启动一个特定的方法。
要检测选择器中的 selected 行(并更新变量),我使用以下代码:
var picked = 15
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if row == 0 {
picked = 1
}
else if row == 1 {
picked = 2
}
else if row == 2 {
picked = 3
}
}
只要用户对选择器进行更改 selection,就会触发此方法。但是,如果用户没有 select 选择器上的任何值(因为用户想要 select 已经 selected 的第一个选项),则不会调用此方法。
我知道关于这个问题已经有一个很好回答的问题,但这是 Objective C (uipickerview-1st-row-selection-does-not-call-didselectrow)。我想要 Swift 的答案,因为我没有 OBjective C 技能。
所以我的问题是:如何让已经 selected 的选择器的第一个选项被用户检测为 selected 行? (所以这个已经-selected-行将更新变量)
我在viewDidAppear
中添加了代码:
picker1.selectRow(2, inComponent: 0, animated: true)
// scrolls the specified row to center.
但这只显示指定的行,而不是 RETURNS didSelectRow 函数的指定行。所以这不适合我
文档说(在 https://developer.apple.com/library/prerelease/ios/documentation/UIKit/Reference/UIPickerViewDelegate_Protocol/index.html#//apple_ref/occ/intfm/UIPickerViewDelegate/pickerView:didSelectRow:inComponent:):"Called by the picker view when the user selects a row in a component."。因此,您应该通过 - (NSInteger)selectedRowInComponent:(NSInteger)component / func selectedRowInComponent(_ component: Int) -> Int 查询选择器本身的状态。
解决此问题的一种简单易用的方法是将每个选择器中的零行设为用户邀请,而不是已处理的值。您可以在零行中放置 'Select' 或“---”之类的内容。这样,用户将被迫与选择器交互,而不会依赖被动地坐在那里的默认值。这在多组件选择器中特别有用,用户可能会更改一些但不是全部组件。