Tag: nstimer

在iPhone中每60秒后调用一次方法

我创build了一个GKSession,创build它的对象后,它开始search设备的可用性 – (void)session:(GKSession *)session peer:(NSString *)peerID didChangeState:(GKPeerConnectionState)state { 我想每60秒后调用一次这个方法,我该怎么办?

Xcode 7.3 / Swift 2:“没有用Objective-Cselect器声明的方法”警告

我一直在使用select器,甚至在迁移到Swift之后,我可以毫无问题地使用它们。 这就是我在Swift 2上使用没有问题,直到我更新Xcode到版本7.3: 正如使用可以看到我使用NSTimer的select器。 这是所谓的行动: func Start () { } 正如你所看到的,Xcode 7.3现在给出了一个警告:“没有用Objective-Cselect器声明的方法”。 通过点击警告,Xcode通过添加“select器”来快速修复代码,但是我仍然得到相同的警告:

在Swift中使用NSTimer

在这种情况下,timerFunc()永远不会被调用。 我错过了什么? class AppDelegate: NSObject, NSApplicationDelegate { var myTimer: NSTimer? = nil func timerFunc() { println("timerFunc()") } func applicationDidFinishLaunching(aNotification: NSNotification?) { myTimer = NSTimer(timeInterval: 5.0, target: self, selector:"timerFunc", userInfo: nil, repeats: true) } }

如何停止NSTimer.scheduledTimerWithTimeInterval

我如何阻止我的计时器运行? 不是一个停顿,而是一个停止。 import UIKit class LastManStandingViewController: UIViewController { @IBOutlet weak var timeLabel: UILabel! @IBOutlet weak var timeTextbox: UITextField! @IBOutlet weak var startButton: UIButton! @IBOutlet weak var stopButton: UIButton! var myCounter = 0 var myTimer : NSTimer = NSTimer() override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a […]

NSTimer与匿名函数/块?

我希望能够在未来安排三个小事件,而不必为每个事件写一个函数。 我如何使用NSTimer来做到这一点? 我明白块促进匿名function,但可以在NSTimer ,如果是的话,如何? [NSTimer scheduledTimerWithTimeInterval:gameInterval target:self selector:@selector(/* I simply want to update a label here */) userInfo:nil repeats:NO];

对NSTimer目标的弱引用防止保留周期

我正在使用这样的NSTimer : timer = [NSTimer scheduledTimerWithTimeInterval:30.0f target:self selector:@selector(tick) userInfo:nil repeats:YES]; 当然, NSTimer保留了创build保留周期的目标。 此外, self不是一个UIViewController,所以我没有像viewDidUnload任何东西,我可以使计时器无效打破周期。 所以我想知道如果我可以使用一个弱引用: __weak id weakSelf = self; timer = [NSTimer scheduledTimerWithTimeInterval:30.0f target:weakSelf selector:@selector(tick) userInfo:nil repeats:YES]; 我听说计时器必须无效(我想从运行循环中释放它)。 但我们可以在我们的dealloc中这样做,对吧? – (void) dealloc { [timer invalidate]; } 这是一个可行的select? 我已经看到很多人处理这个问题的方法,但我没有看到这个。

如何停止NStimer事件?

可能重复: NSTimer不停止 在我的应用程序中,我正在使用NStimer每3秒调用一次animation函数。 我想停止这个定时器,并在定时器仍在运行时调用另一个事件。 这可能吗?

在目标c中以自定义格式获取string中的当前时间

我希望当前时间以string的格式如下。 dd-mm-yyyy HH:MM 怎么样?

确定UIView是否对用户可见?

是否有可能确定我的UIView是否对用户可见? 我的视图作为subview视图被添加到Tab Bar Controller 。 这个视图的每个实例都有一个NSTimer来更新视图。 但是我不想更新用户不可见的视图。 这可能吗? 谢谢

将parameter passing给由NSTimer调用的方法

我怎样才能传递一个参数到NSTimer调用的方法? 我的计时器看起来像这样: [NSTimer scheduledTimerWithTimeInterval:4 target:self selector:@selector(updateBusLocation) userInfo:nil repeats:YES]; 我想能够传递一个string的方法updateBusLocation。 另外,我应该在哪里定义方法updateBusLocation? 在创build计时器的同一个.m文件中? 编辑: 其实我还有问题 我收到错误消息: 终止应用程序由于未捕获的exception“NSInvalidArgumentException”,原因:' * – [MapKitDisplayViewController updateBusLocation]:无法识别的select器发送到实例0x4623600' 这是我的代码: – (IBAction) showBus { //do something [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateBusLocation) userInfo:txtFieldData repeats:YES]; [txtFieldData release]; } – (void) updateBusLocation:(NSTimer*)theTimer { NSLog(@"timer method was called"); NSString *txtFieldData = [[NSString alloc] initWithString:(NSString*)[theTimer userInfo]]; if(txtFieldData == busNum.text) { //do […]