1 #import "ViewController.h" 2 3 static NSInteger count = 0; 4 5 @interface ViewController () 6 7 @property (weak, nonatomic) IBOutlet UILabel *label; 8 9 @property (nonatomic, strong)NSArray *array; 10 /** 添加定时器 */ 11 @property (nonatomic, strong) NSTimer *time; 12 13 @end 14 15 @implementation ViewController 16 17 - (NSArray *)array 18 { 19 if(!_array) 20 { 21 _array = [NSArray arrayWithObjects:@"1...",@"2....",@"3.....",@"4......", nil]; 22 } 23 return _array; 24 } 25 26 - (void)viewDidLoad { 27 [super viewDidLoad]; 28 29 self.time = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(runLabel) userInfo:nil repeats:YES]; 30 [self.time fire]; // 立即触发定时器 31 // [time setFireDate:[NSDate distantPast]]; // 开启定时器 32 // [time setFireDate:[NSDate distantFuture]]; // 暂停定时器 33 } 34 35 - (void)runLabel 36 { 37 self.label.text = self.array[count++ % self.array.count]; 38 } 39 40 /** 移除定时器 */ 41 - (void)removeTimer 42 { // 定时器一旦被停止,不会重新开始,要被移除。 43 [self.time invalidate]; 44 self.time = nil; 45 } 46 - (void)didReceiveMemoryWarning { 47 [super didReceiveMemoryWarning]; 48 // Dispose of any resources that can be recreated. 49 } 50 51 @end