gpt4 book ai didi

swift - 如何在 60 秒后取消 NSTimer?

转载 作者:行者123 更新时间:2023-11-30 13:05:28 25 4
gpt4 key购买 nike

我有一个按钮操作将数据发送到 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)
}

最佳答案

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

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

swift 2

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

swift 3、4、5

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

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

关于swift - 如何在 60 秒后取消 NSTimer?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39427835/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com