0

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

4

2 に答える 2

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.

    }
}
于 2013-05-24T11:33:17.070 に答える