Swift / SO新手在这里 - 会尽力清楚!
我从@IBAction
调用wkinterfacepicker
。这会调用一个可能会再次更改选择器的函数 - 这会导致@IBAction
再次被触发,从而导致不必要的循环类型行为。
我要做的是让控件传递到第二个函数,而不是从>调用 @IBAction
函数。我有一个使用dispatch_after
的工作解决方案,实现任意延迟,以便@IBAction
在第二个func开始之前完成,但这似乎不是非常优雅/合适。
这大致是这样的:
@IBAction func freePickerChanged(value: Int) {
selectedValue = value
let triggerTime = (Int64(NSEC_PER_SEC) * 1)
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, triggerTime), dispatch_get_main_queue(), { () -> Void in
self.changeValues()
})
// Without using dispatch_after, program control returns here from changeValues(), causing loop
}
//Ideally would call changeValues() here, after @IBAction exits
func changeValues() {
//Do some calculations here. Perhaps the picker needs to be changed again using setSelectedItemIndex - @IBACTION KICKED OFF AGAIN
}
有没有更好的方法将控件从@IBAction func传递到第二个函数?或者我可能完全走错了轨道?
提前感谢您花时间看看这个!
答案 0 :(得分:1)
除了选择运行代码之后的随机延迟,另一种方法是将代码块异步地放入主队列。这将在当前运行循环完成后运行,因此您不必计算等待的时间。
,而不是
ReplaySubject
试
dispatch_after(...