calling a method after each 60 seconds in iPhone

2019-01-16 23:51发布

I have created an GKSession and as its object is created, it starts search for availability of devices, as

 - (void)session:(GKSession *)session peer:(NSString *)peerID didChangeState:(GKPeerConnectionState)state {

I want to call this method after each 60 seconds, what should I do?

5条回答
淡お忘
2楼-- · 2019-01-17 00:22

Once you set NSTimer to scheduleWithTimeInterval it calls it immediately. You can use

  [self performSelector:@selector(doSomething) withObject:nil afterDelay:60.0f];
查看更多
贼婆χ
3楼-- · 2019-01-17 00:28

Swift 3:

Timer.scheduledTimer(withTimeInterval: 60, repeats: true, block: { (timer) in 
print("That took a minute")
})
查看更多
时光不老,我们不散
4楼-- · 2019-01-17 00:31

Use the following code:

 NSTimer* timer = [NSTimer scheduledTimerWithTimeInterval: 60.0 target: self
                               selector: @selector(timerFired:) userInfo: nil repeats: YES];

 - (void)timerFired:(NSTimer*)theTimer{
 if(condition){
       [theTimer isValid]; //recall the NSTimer
       //implement your methods
  }else{
      [theTimer invalidate]; //stop the NSTimer
 }
}
查看更多
再贱就再见
5楼-- · 2019-01-17 00:37

You can use schedular method...

-(void) callFunction:(CCTime)dt 
{
    NSLog(@"Calling...");
}

you can call above function by using this...

[self schedule:@selector(callFunction:) interval:60.0f];
查看更多
你好瞎i
6楼-- · 2019-01-17 00:41

Use NSTimer

NSTimer* myTimer = [NSTimer scheduledTimerWithTimeInterval: 60.0 target: self
                                   selector: @selector(callAfterSixtySecond:) userInfo: nil repeats: YES];

After each 60.0 second , iOS will call the below function

-(void) callAfterSixtySecond:(NSTimer*) t 
{
    NSLog(@"red");
}
查看更多
登录 后发表回答