我有以下问题:我有一个包含5个对象的NSArray
。我想使用NSTimer
显示每个项目2秒钟,并在显示数组中的最后一项后释放计时器。
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
例外。
答案 0 :(得分:1)
这样做,
在.h
int tempValue;
在.m
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 {
if(tempValue< Country.count) {
// Display array object eg: label.text=[Country objectAtIndex:tempValue];
tempValue++;
} else {
[theTimer invalidate];
theTimer=nil;
// or you can reset the tempValue into 0.
}
}
答案 1 :(得分:0)
#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 alloc]initWithObjects:@"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 {
if(tempValue< Country.count) {
NSString *objectname=[Country objectAtIndex:tempValue];
NSLog(@"objectname is %@",objectname);
tempValue++;
} else {
[theTimer invalidate];
theTimer=nil;
// or you can reset the tempValue into 0.
}
}
- (void)viewDidUnload {
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
@end
感谢Tony并且此代码正常运行