Ios 如何在swift中重新启动计时器

Ios 如何在swift中重新启动计时器,ios,swift,nstimer,Ios,Swift,Nstimer,我正在使用OTP屏幕制作一个应用程序。我使用计时器功能进行了锻炼,还能够更改文本并再次显示计时器。我的问题是我无法重新启动计时器。它卡在00:10中 下面是我的代码,请帮忙 @IBOutlet weak var butttonName: ButtonDesign! var countTimer:Timer! var counter = 10 var restartTimer = false var isTimerRunning = false override func viewDidLoad

我正在使用OTP屏幕制作一个应用程序。我使用计时器功能进行了锻炼,还能够更改文本并再次显示计时器。我的问题是我无法重新启动计时器。它卡在
00:10

下面是我的代码,请帮忙

@IBOutlet weak var butttonName: ButtonDesign!
var countTimer:Timer!
var counter = 10
var restartTimer = false
var isTimerRunning = false

override func viewDidLoad() {
if isTimerRunning == false {
        startTimer()}
}

func startTimer() {
self.countTimer = Timer.scheduledTimer(timeInterval: 1 , target: self, selector: #selector(OTPVerificationViewController.changeTitle), userInfo: nil, repeats: true)
isTimerRunning = true
}
@objc func changeTitle()
{
    if counter != 0
    {
        butttonName.setTitle(timeString(time: TimeInterval(counter)), for: .normal)
        counter -= 1
        butttonName.isEnabled = false

    }  else {
        countTimer.invalidate()
        butttonName.setTitle("Resend", for: .normal)
    }
}

@IBAction func verifyButton(_ sender: UIButton) {
if butttonName.currentTitle == "Resend" {
        print("clicked when name is resend !!!")
        if restartTimer == true {
            startTimer()
            restartTimer = false
        }
    } else {
        print("clicked when it is name is verify")
        self.performSegue(withIdentifier: "confirmPassword", sender: self)
    }
}

我希望在用户单击“重新发送”按钮时重新启动计时器,并希望在用户单击“验证”按钮时执行segue。两个按钮都是同一个,我正在运行时更改名称

您也需要重新启动计数器

if butttonName.currentTitle == "Resend" {
   counter = 10
   // startTimer()
}

只需按照如下方法进行操作:

 var count = 120
func updateCounter(){
    if #available(iOS 10.0, *) {
        Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true, block: { (timer) in
            if(self.count > 0){
                self.lblTime.text = "\(self.count)"
                self.count = self.count-1
            }else {
                self.lblTime.text =  "0"
                timer.invalidate()
                self.btnresend.isHidden = false // in your case you can change the title of the button
            }
        })
    } else {
        // Fallback on earlier versions
        Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(self.timerMethod), userInfo: nil, repeats: true)
    }
}

@objc func timerMethod() {
    if(self.count > 0){
        self.lblTime.text = "\(self.count)"
        self.count = self.count-1
    }else {
        self.lblTime.text =  "0:0"
        self.btnresend.isHidden = false
    }
}
从您想要重新启动时间的地方调用update counter方法即可

设置计数120或您想要的

self.count = 120

您可以再次调用您的
startTimer()
函数,因为您在Varify按钮上调用StartTime,这里您犯了2个错误-1。您从未将restartTimer设置为false。2.您必须再次增加计数器值10。在-else{countTimer.invalidate()butttoname.setTitle(“Resend”,for:.normal)}中将restartTimer设置为true,并在startTime()函数调用中设置计数器值10。非常感谢您的快速响应它工作了。。。只有一句话让我从早上开始绞尽脑汁,除此之外,你能告诉我我编码的所有内容都是正确的吗?我认为你需要删除restartTimer变量,因为它没有用,而且声明计时器是可选的是一个很好的做法,比如var countTimer:timer?我遇到了一个问题。该代码不适用于performsgue。我无法理解它为什么会产生问题,因为它正在发挥作用。它总是执行这个序列。。按钮名称是重新发送还是验证。。不过我还是会想办法的。因为这件事我以前也做过。谢谢你