我有以下问题:我有一个NSArray
包含 5 个对象的对象。我想使用 显示每个项目 2 秒NSTimer
,并在显示数组中的最后一个项目后释放计时器。
Country = [NSArray arrayWithObjects:@"Aus",@"India",@"Pak",@"China",@"Japan", nil];
cntcount = [Country count];
NSLog(@"concount=%i", cntcount);
[NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(startTime) userInfo:nil repeats:YES];
但我不会再迈出一步。我应该在starttime()
函数中做什么?请帮忙!
//
// AppViewController.m
// NSTimerExample
//
#import "AppViewController.h"
@interface AppViewController ()
@end
@implementation AppViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
Country = [NSArray arrayWithObjects:
@"Aus", @"India", @"Pak", @"China", @"Japan", nil];
tempValue = 0;
NSTimer *popupTimer = [NSTimer scheduledTimerWithTimeInterval:2.0
target:self
selector:@selector(startTime:)
userInfo:nil
repeats:YES];
NSRunLoop *runner = [NSRunLoop currentRunLoop];
[runner addTimer:popupTimer forMode:NSDefaultRunLoopMode];
}
- (void)startTime:(NSTimer *)theTimer {
int cntcount = [Country count];
if(tempValue < cntcount) {
NSString *objectName = [Country objectAtIndex:tempValue];
NSLog(@"objectName=%@", objectName);
tempValue++;
} else {
[theTimer invalidate];
theTimer = nil;
// or you can reset tempValue to 0.
}
}
显示最后一项后,我想释放计时器,因为不再需要它,但我得到了EXC_BAD_ACCESS
例外。