如何在按钮被点击后禁用它几秒钟

3
以下是我想在点击后禁用5秒钟的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,除了那里没有对按钮的引用。也许有一种方法可以做到,但我不知道怎么办。
提前感谢您的帮助。

1
我猜你想这样做是因为你在等待异步操作完成。五秒钟不好,因为它可能比实际完成操作所需的时间更多或更少。既然你已经有了一个回调块,为什么不在调用saveInBackgroundWithBlock()之前禁用按钮,然后在完成块内重新启用它,如果你决定不关闭视图控制器(这会销毁该按钮)。 - i_am_jorf
@i_am_jorf 嗯,有点这样。我这么做是因为如果连接速度慢,用户可以点击几次按钮,并发布相同的内容。 - James Chen
4个回答

5
尝试以下操作:
  1. 在点击动作设置按钮时禁用它。
  2. 使用延迟函数设置一些时间间隔,以便启用您的按钮。
例如:
let tapGesture = UITapGestureRecognizer(target: self, action: "tapaction")
tapGesture.numberOfTapsRequired = 1
Your_Button_name.addGestureRecognizer(tapGesture)


@IBAction func tapaction()
{
    Your_Button_name.disable=true
    //Delay function to enable your button
    NSTimer.scheduledTimerWithTimeInterval(Your_time_value_delay, target: self, selector: Selector("enablefunc"), userInfo: nil, repeats: false)
}

func enablefunc()
{
   Your_Button_name.disable=false
}

2

SWIFT 5.2

在UIButton的Action中放置以下代码:

yourButtonOutlet.isEnabled = false
                //Delay function to enable your button for 3 seconds
            Timer.scheduledTimer(timeInterval: 3, target: self, selector: #selector(self.enablefunc), userInfo: nil, repeats: false)

在UIButton函数之外添加以下内容:

@objc func enablefunc()
{
    yourButtonOutlet.isEnabled = true
}

1
   var disableMyButton = sender as? UIButton
   disableMyButton!.enabled = false
   testObject.saveInBackgroundWithBlock({ (success, error) -> Void in
        if success {
            self.dismissViewControllerAnimated(true , completion: nil)
            if let completion = self.completion {
                completion(self)
            }
        } else {
            disableMyButton!.enabled = true
        }
    }

此外,根据 completion(self) 的功能,你可能希望在关闭视图控制器之前执行它。我不确定。

如果成功,视图控制器(包含按钮的那个)将被解散。那么你为什么要重新启用它呢?我的意思是,如果你愿意,你可以在 if success { 之前就这样做。假设我对你的代码结构一无所知,除了你发布的内容。今天我的心灵能量有点不足。 :) - i_am_jorf
嗯,当我尝试这个时,所有东西都崩溃了。 - James Chen

1

您可以使用调度框架来添加延迟。请使用主线程。

disableMyButton!.enabled = false
let delay = dispatch_time(DISPATCH_TIME_NOW, Int64(5 * Double(NSEC_PER_SEC)))

dispatch_after(delay, dispatch_get_main_queue()) {
    disableMyButton!.enabled = true
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接