次の問題があり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
例外が発生しています。