以下是我想在点击后禁用5秒钟的IBAction代码。
但是这样做行不通,因为它是IBAction,除了那里没有对按钮的引用。也许有一种方法可以做到,但我不知道怎么办。
提前感谢您的帮助。
@IBAction func postPressed(sender: AnyObject) {
//var disableMyButton = sender as? UIButton
//disableMyButton!.enabled = false
//NSTimer.scheduledTimerWithTimeInterval(5, target: self, selector:"enableButton", userInfo: nil, repeats: false)
if(currLocation != nil){
let testObject = PFObject(className: "Hey")
testObject["text"] = self.postView.text
testObject["count"] = 0
testObject["replies"] = 0
testObject["location"] = PFGeoPoint(latitude: currLocation!.latitude , longitude: currLocation!.longitude)
testObject["comments"] = []
testObject["user"] = PFUser.currentUser()
testObject.saveInBackgroundWithBlock({ (success, error) -> Void in
if success {
self.dismissViewControllerAnimated(true , completion: nil)
if let completion = self.completion {
completion(self)
}
}
})
} else {
alert()
}
}
注释掉的代码是我试图在5秒内禁用按钮,但这段代码崩溃了,我认为它崩溃了是因为没有引用启用按钮。
通常我会做类似这样的事情
func enableButton() {
self.button.enabled = true
}
但是这样做行不通,因为它是IBAction,除了那里没有对按钮的引用。也许有一种方法可以做到,但我不知道怎么办。
提前感谢您的帮助。
saveInBackgroundWithBlock()
之前禁用按钮,然后在完成块内重新启用它,如果你决定不关闭视图控制器(这会销毁该按钮)。 - i_am_jorf