从另一个角度停止 NSTimer?

Stop NSTimer from another view?

我想从另一个视图 UIRequiredPassowrdViewController 中停止 NSTimer SettingsTabelViewController,所以我首先创建倒计时:

class UIRequiredPassowrdViewController: UIViewController, UITextFieldDelegate {
    var timer: NSTimer?
    var remainingSeconds = 1000

    override func viewDidLoad() {
        super.viewDidLoad()

        timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: "updateTimer:", userInfo: nil, repeats: true)
    }

    func updateTimer(timer: NSTimer) {
        remainingSeconds -= 1
        println(remainingSeconds)
    }

    func StopTimerNotication(){
        println("Function Start!")
        timer?.invalidate()
        timer = nil
    }
}

然后我从另一个视图调用函数 StopTimerNotication() SettingsTabelViewController :

@IBAction func myButtonClicked(sender: AnyObject) {
    println("Button Clicked")
    var myClass: UIRequiredPassowrdViewController=UIRequiredPassowrdViewController()
    myClass.StopTimerNotication()
}

我 运行 应用程序,日志正确显示倒计时,单击按钮和 运行 功能,但计时器就是不停止:

999
998
997
996
995
994
993
992
991
990
989
Button Clicked
Function Start!
988
987
986
985

我该如何解决这个问题?

在 AppDelegate 中声明 NSTimer 为全局。

var timer: NSTimer? 

在你的UIRequiredPassowrdViewControllerclass

class UIRequiredPassowrdViewController: UIViewController, UITextFieldDelegate {

    var remainingSeconds = 1000

    override func viewDidLoad() {
        super.viewDidLoad()

        let appDelegate = (UIApplication.sharedApplication().delegate as! AppDelegate)

        appDelegate.timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: "updateTimer:", userInfo: nil, repeats: true)
    }

    func updateTimer(timer: NSTimer) {
        remainingSeconds -= 1
        println(remainingSeconds)
    }
}

SettingsTabelViewController 中有您想要停止的按钮,如下所示。

@IBAction func myButtonClicked(sender: AnyObject) {

  let appDelegate = (UIApplication.sharedApplication().delegate as! AppDelegate)
  appDelegate.timer?.invalidate()   

}

myClass是一个新初始化的view controller,它和你启动定时器的view controller完全不同。所以要解决你的问题,基本上你需要找到一种方法来告诉特定的视图控制器停止它的计时器。
这种情况有两种常见的解决方案:
(1) 使用通知:在你的UIRequiredPassowrdViewController注册一个特定的通知,我们称它为StopTimerNotification,当你收到这样的通知时停止计时器。并在SettingsTabelViewController中,在myButtonClicked中发出StopTimerNotification
(2) 使用委托:这里你需要一个简单的协议,我们称它为StopTimerProtocol,它可能只有方法func stopTimer()。而你的UIRequiredPassowrdViewController需要遵守这个协议,实现那个方法。然后在SettingsTabelViewController中添加一个delegate 属性,这是id<StopTimerProtocol>的一种类型。之后,当您在 UIRequiredPassowrdViewController 之上呈现或按下 SettingsTabelViewController 时,设置 delegate 属性。最后,在 SettingsTabelViewControllermyButtonClicked 只需调用 self.delegate.stopTimer()

Kirit 所说的正是 Objective-C 中的代码,供将来可能偶然发现此内容的任何人使用 post

//  AppDelegate.h
@property (nonatomic, retain) NSTimer *soundTimer;

//  ViewController.m
#import "AppDelegate.h"

//start timer - call it from anywhere in your ViewController
AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
    appDelegate.soundTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(playButtonSound1) userInfo:nil repeats:NO];

//stop timer - call it from anywhere in your ViewController
AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
    [appDelegate.soundTimer invalidate];
    appDelegate.soundTimer = nil;