Iphone 如何使用2 N计时器的循环?

Iphone 如何使用2 N计时器的循环?,iphone,ios5,xcode4.3,Iphone,Ios5,Xcode4.3,有人想教我如何使用NSTimer使用循环过程吗?我有两个进程计时器要做,计时器1和计时器2重复到n循环(计时器1->计时器2->计时器1->计时器2等等,直到n循环),由按钮触发。我不熟悉xcode。请教我。两个定时器都有用户输入。如果可能的话,请给我举个例子 我的代码应该是这样的。如果我错了,就纠正我 - (void) timer2Elapsed:(NSTimer *) timer; { ... displayCountDown.text = [NSString string

有人想教我如何使用
NSTimer
使用循环过程吗?我有两个进程计时器要做,计时器1和计时器2重复到n循环(计时器1->计时器2->计时器1->计时器2等等,直到n循环),由按钮触发。我不熟悉xcode。请教我。两个定时器都有用户输入。如果可能的话,请给我举个例子

我的代码应该是这样的。如果我错了,就纠正我

- (void) timer2Elapsed:(NSTimer *) timer;
{
    ...

    displayCountDown.text = [NSString stringWithFormat:@"%d : %d : %d", hour , minute, second];
}

- (void)timer1Elapsed: (NSTimer *) timer;
{
    ...

    displayCountDown.text = [NSString stringWithFormat:@"%d : %d : %d", hour , minute, second];
}
触发倒计时的按钮:

- (IBAction)startBtn:(id)sender {
        endSetTime = [NSDate timeIntervalSinceReferenceDate] + totalSecondTime;
        endSetRest = [NSDate timeIntervalSinceReferenceDate] + totalSecondRest;
        countdownTimer = [NSTimer scheduledTimerWithTimeInterval: 0.99 target: self selector: @selector(timer1Elapsed:) userInfo: nil repeats: YES];

}
这里有人告诉我使用这个代码,但我不知道我应该在里面写什么,把它放在哪里?他说是因为循环?我不知道?如何连接我按钮中的代码

+ (void) startTimer:(id)timer
{
    static int numberOfTimes = 0;
    if(numberOfTimes >= 5)
    {
        numberOfTimes = 0;
        return;
    }
    numberOfTimes ++;

    [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(timer1Elapsed:) userInfo:nil repeats:NO];
}

忘记计时器。使用performSelector:withObject:afterDelay:selector

@interface CECountdown : NSObject {
    NSDate *_startDate;
    NSTimeInterval _countdownInterval;
}

@end

@implementation CECountdown

- (id)initWithCountdownInterval:(NSTimeInterval)interval {
    self = [super init];

    if(self) {
        _countdownInterval = interval;
        _startDate = [[NSDate date] retain];
    }

    return self;
}

- (void)dealloc {
    [_startDate release];

    [super dealloc];
}

- (void)method0 {
    // Do something
    [self performSelector:@selector(method1) withObject:nil afterDelay:0.3];
    [self checkTimeout];
}

- (void)method1 {
    // Do something
    [self performSelector:@selector(method0) withObject:nil afterDelay:0.3];    
    [self checkTimeout];
}

- (void)invalidate {
    [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(method1) object:nil];
    [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(method2) object:nil];
}

- (void)checkTimeout {
    if([NSDate timeIntervalSinceReferenceDate] - [_startDate timeIntervalSinceReferenceDate] > _countdownInterval) {
        [self invalidate];
    }
}

- (void)didTapStartButton:(id)sender {
    [self performSelector:@selector(method1) withObject:nil afterDelay:0.3];
}

@end

对不起,是否有可能使用nstimer循环?因为如果我使用PerformSelect,我不知道如何更改NSTimer中的代码。在我的NSTimer中是这样的:NSTimeInterval remainingTime=endSetRest-[NSDate timeIntervalSinceReferenceDate];时间间隔划痕;如果(remainingTime您仍然可以通过将开始时间存储在实例变量中来计算剩余时间。我不明白使用此方法有什么不可行的地方。我认为不可能按您希望的方式循环使用NSTimer。嘿,如何使用invalidate?它们倒计时为负数。我应该将该invalidate放在何处?如果它们出现,我希望invalidate同样,您的代码正确吗?为什么在方法1中使用@selector(方法1)?为什么不使用@selector(方法2)?而在方法0中,您使用@selector(方法2)?我的时间倒计时只在method1,但它不能继续到您的method0,为什么?而且时间滴答有时似乎有延迟?对不起,我已经在使用您的代码,但它仍然不正确。您的代码正在调用2进程,同时运行它,即method0和method1,我想您忘记为循环进程编写代码,因为我没有看到“x”的任何循环(只说5次。但是谢谢你,至少你把我带到最近的一个。我只是想也许我的案子还没有被一些人审理过。