Guest User

Untitled

a guest
Feb 18th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.21 KB | None | 0 0
  1. -(void)btnClick:(id)sender
  2.  
  3. {
  4. NSLog(@"btnClick");
  5.  
  6.  
  7. dispatch_queue_t queue=dispatch_get_global_queue(0, 0);
  8. dispatch_source_t timer=dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
  9. dispatch_source_set_timer(timer, dispatch_walltime(NULL, 0), 2*NSEC_PER_SEC, 1*NSEC_PER_SEC);
  10.  
  11. dispatch_source_set_event_handler(timer, ^{
  12.  
  13. dispatch_async(dispatch_get_main_queue(), ^{
  14. self.label1.text=[NSString stringWithFormat:@"%d", arc4random_uniform(1000000)];
  15. });
  16. });
  17.  
  18. dispatch_resume(timer);
  19. }
  20.  
  21. //Define member varialbe for timer in header file
  22. NSTimer* timerForLocationUpdate;
  23.  
  24. //Call createTimer to create timer that will execute in each 1 min
  25.  
  26. - (void) createTimer
  27. {
  28. timerForLocationUpdate = [NSTimer scheduledTimerWithTimeInterval:60 /*Number of seconds*/
  29. target:self
  30. selector:@selector(updateLocation)
  31. userInfo:nil
  32. repeats:YES];
  33.  
  34. [[NSRunLoop currentRunLoop] addTimer:timerForLocationUpdate
  35. forMode:NSRunLoopCommonModes];
  36. }
  37.  
  38. //Method is called in each 1 min
  39. - (void) updateLocation
  40. {
  41.  
  42. }
  43.  
  44. NSTimer *t = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(yourMethod) userInfo:nil repeats:YES];
  45. NSRunLoop *runner = [NSRunLoop currentRunLoop];
  46. [runner addTimer:t forMode: NSDefaultRunLoopMode];
  47.  
  48. -(void)yourMethod{
  49. Self.label1.text = [NSString stringWithFormat:@"%d", arc4random_uniform(1000000)];
  50. }
  51.  
  52. -(void)btnClick:(id)sender
  53.  
  54. {
  55. NSLog(@"btnClick");
  56.  
  57. static dispatch_source_t timer;
  58.  
  59. dispatch_queue_t queue=dispatch_get_global_queue(0, 0);
  60. timer=dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
  61. dispatch_source_set_timer(timer, dispatch_walltime(NULL, 0), 2*NSEC_PER_SEC, 1*NSEC_PER_SEC);
  62.  
  63. dispatch_source_set_event_handler(timer, ^{
  64.  
  65. dispatch_async(dispatch_get_main_queue(), ^{
  66. self.label1.text=[NSString stringWithFormat:@"%d", arc4random_uniform(1000000)];
  67. });
  68. });
  69.  
  70. dispatch_resume(timer);
  71. }
Add Comment
Please, Sign In to add comment