以编程方式设置 WKPickerItem 不会超过某个值?
Programmatically setting WKPickerItem won't go above a certain value?
想不通,通过下面的代码,我让用户在 WKPickerItem
上设置一个值,将其存储在 UserDefaults
,然后在下次 [=] 时加载它13=] 已加载并尝试使用存储的值设置 PickerItem。问题是,如果我将该值设置为 1 - 7,然后导航回来然后再次点击以显示此界面控制器,我会在 InterfacePicker 中看到正确的值。但是,如果我将它设置为 8、9 或 10,InterfacePicker 只会上升到 7?正确的索引和项目打印到控制台,只是 UI 中的实际值是错误的?
import WatchKit
import Foundation
class SettingsInterfaceController: WKInterfaceController {
let defaults = UserDefaults.standard
var shiftTimerHapticIntervalOptions = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
var userShiftTimerHapticInterval = 10
@IBOutlet var shiftTimerHapticIntervalPicker: WKInterfacePicker!
@IBAction func shiftTimerHapticIntervalDidChange(_ value: Int) {
WKInterfaceDevice.current().play(.click)
userShiftTimerHapticInterval = shiftTimerHapticIntervalOptions[value]
}
override func awake(withContext context: Any?) {
super.awake(withContext: context)
let shiftTimerHapticIntervalPickerItems: [WKPickerItem] = shiftTimerHapticIntervalOptions.map {
let pickerItem = WKPickerItem()
pickerItem.caption = [=10=].description
pickerItem.title = [=10=].description
return pickerItem
}
shiftTimerHapticIntervalPicker.setItems(shiftTimerHapticIntervalPickerItems)
}
override func willActivate() {
// This method is called when watch view controller is about to be visible to user
super.willActivate()
// Look up user settings
if let userShiftTimerHapticIntervalUnwrapped = defaults.value(forKey: userShiftTimerHapticIntervalKey) as? Int {
userShiftTimerHapticInterval = userShiftTimerHapticIntervalUnwrapped
if let indexOfUserShiftTimerHapticInterval = shiftTimerHapticIntervalOptions.index(of: userShiftTimerHapticInterval) { shiftTimerHapticIntervalPicker.setSelectedItemIndex(indexOfUserShiftTimerHapticInterval)
}
}
}
override func didDeactivate() {
// This method is called when watch view controller is no longer visible
super.didDeactivate()
defaults.set(userShiftTimerHapticInterval, forKey: userShiftTimerHapticIntervalKey)
}
}
尝试调用
shiftTimerHapticIntervalPicker.setSelectedItemIndex(indexOfUserShiftTimerHapticInterval)
在 InterfaceController 的 didAppear()
函数中,而不是在 willActivate()
函数中。当试图在 willActivate()
函数中设置选择器的索引时,我看到了一些相同的东西,但更随机。
想不通,通过下面的代码,我让用户在 WKPickerItem
上设置一个值,将其存储在 UserDefaults
,然后在下次 [=] 时加载它13=] 已加载并尝试使用存储的值设置 PickerItem。问题是,如果我将该值设置为 1 - 7,然后导航回来然后再次点击以显示此界面控制器,我会在 InterfacePicker 中看到正确的值。但是,如果我将它设置为 8、9 或 10,InterfacePicker 只会上升到 7?正确的索引和项目打印到控制台,只是 UI 中的实际值是错误的?
import WatchKit
import Foundation
class SettingsInterfaceController: WKInterfaceController {
let defaults = UserDefaults.standard
var shiftTimerHapticIntervalOptions = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
var userShiftTimerHapticInterval = 10
@IBOutlet var shiftTimerHapticIntervalPicker: WKInterfacePicker!
@IBAction func shiftTimerHapticIntervalDidChange(_ value: Int) {
WKInterfaceDevice.current().play(.click)
userShiftTimerHapticInterval = shiftTimerHapticIntervalOptions[value]
}
override func awake(withContext context: Any?) {
super.awake(withContext: context)
let shiftTimerHapticIntervalPickerItems: [WKPickerItem] = shiftTimerHapticIntervalOptions.map {
let pickerItem = WKPickerItem()
pickerItem.caption = [=10=].description
pickerItem.title = [=10=].description
return pickerItem
}
shiftTimerHapticIntervalPicker.setItems(shiftTimerHapticIntervalPickerItems)
}
override func willActivate() {
// This method is called when watch view controller is about to be visible to user
super.willActivate()
// Look up user settings
if let userShiftTimerHapticIntervalUnwrapped = defaults.value(forKey: userShiftTimerHapticIntervalKey) as? Int {
userShiftTimerHapticInterval = userShiftTimerHapticIntervalUnwrapped
if let indexOfUserShiftTimerHapticInterval = shiftTimerHapticIntervalOptions.index(of: userShiftTimerHapticInterval) { shiftTimerHapticIntervalPicker.setSelectedItemIndex(indexOfUserShiftTimerHapticInterval)
}
}
}
override func didDeactivate() {
// This method is called when watch view controller is no longer visible
super.didDeactivate()
defaults.set(userShiftTimerHapticInterval, forKey: userShiftTimerHapticIntervalKey)
}
}
尝试调用
shiftTimerHapticIntervalPicker.setSelectedItemIndex(indexOfUserShiftTimerHapticInterval)
在 InterfaceController 的 didAppear()
函数中,而不是在 willActivate()
函数中。当试图在 willActivate()
函数中设置选择器的索引时,我看到了一些相同的东西,但更随机。