从另一个视图停止NSTimer?

时间:2015-06-26 06:09:23

标签: ios swift cocoa-touch nstimer

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

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

我该如何解决这个问题?

3 个答案:

答案 0 :(得分:4)

在AppDelegate中将NSTimer声明为全局。

var timer: NSTimer? 

在您的UIRequiredPassowrdViewController班级

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()   

}

答案 1 :(得分:4)

myClass是一个新初始化的视图控制器,它与启动计时器的视图控制器完全不同。因此,要解决您的问题,基本上您需要找到一种方法来告诉特定的视图控制器停止其计时器 这种情况有两种常见的解决方案:
(1)使用通知:在UIRequiredPassowrdViewController注册特定通知的情况下,我们将其称为StopTimerNotification,当您收到此类通知时,请停止计时器。在SettingsTabelViewController中,在StopTimerNotification中发送myButtonClicked (2)使用委托:你需要一个简单的协议,我们称之为StopTimerProtocol,它可能只有方法func stopTimer()。并且您的UIRequiredPassowrdViewController需要符合此协议并实施该方法。然后添加delegate属性id<StopTimerProtocol>SettingsTabelViewController。之后,当您在SettingsTabelViewController之上展示或推送UIRequiredPassowrdViewController时,请设置delegate属性。最后,在SettingsTabelViewControllermyButtonClicked只需致电self.delegate.stopTimer()

答案 2 :(得分:0)

除了代码之外,kirit所说的除了代码是针对未来可能偶然发现这篇文章的任何人的Objective-C

//  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;