2016-09-10 50 views
0

我有一個將數據發送給Parse的按鈕操作。一旦按下按鈕,標題將變爲取消,當按下「取消」按鈕時,數據將從Parse中刪除。我想知道如何在60秒後用NSTimer自動取消。我有一個數組設置太(VAR isCalling = FALSE)如何在60秒後取消NSTimer?

// 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) 
        } 

回答

0

你應該使用NSTimer.scheduledTimerWithTimeInterval,只需更換這行:

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

如果你想

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

NSTimer Class Reference瞭解更多關於創建計時器的信息。

+0

它現在有效。謝謝! –