我有一个名为myAction的函数,当我执行它会花费很多时间。
那么,如何在myAction函数中首先更新textFiled?
func doAction() {
// do this action will take a lot of time
}
@IBAction func myAction(sender: AnyObject) {
// update textField first
self.textFild?.stringValue = "start do action!"
doAction()
// update textField when doAction finished
self.textFild?.stringValue = "finished action!"
}
答案 0 :(得分:0)
更好的做法是在doAction()
中使用完成处理程序,然后在完成后,您可以将textField
更新为"finished action"
。在"start do action!"
本身设置viewDidLoad()
,以便textField在视图加载后立即读取"start do action!"
,如下所示:
class someClass: ... {
override func viewDidLoad(){
super.viewDidLoad()
self.textField?.text = "start do action"
//your code
}
func doAction(completion: (finished: Bool) -> ()){
//your code
completion(finished: true)
}
@IBAction func myAction(sender: AnyObject) {
doAction{success in
if success{
dispatch_async(dispatch_get_main_queue(), {
self.textField?.text = "finished action!" //Update on main thread
})
}
else{
//do something else
}
}
}
这应该有效。试试吧:))