如何在60秒后取消NSTimer?

时间:2016-09-10 15:52:35

标签: swift nstimer

我有一个将数据发送到Parse的按钮操作。按下按钮后,标题将变为取消,当"取消"按下按钮,数据从Parse中删除。我想知道如何在60秒后使用NSTimer自动取消。我也有一个数组设置(var isCalling = false)

// Function called by within NSTimer in button action

func refresh(){
    self.callButtonTapped(nil)
    !isCalling
}

// Within the button action

   if error == nil {
                        //Success
                        self.isCalling = true
                        self.callButton.setTitle("Cancel", forState: UIControlState.Normal)
                        self.timer = NSTimer(timeInterval: 60.0, target: self, selector: #selector (self.refresh), userInfo: nil, repeats: false)
                    }

1 个答案:

答案 0 :(得分:0)

您应该使用NSTimer.scheduledTimerWithTimeInterval,只需替换此行:

self.timer = NSTimer(timeInterval: 60.0, target: self, selector: #selector (self.refresh), userInfo: nil, repeats: false)

self.timer = NSTimer.scheduledTimerWithTimeInterval(60.0, target: self, selector:  #selector (self.refresh), userInfo: nil, repeats: false)

如果您想了解有关创建计时器的更多信息,请阅读NSTimer Class Reference