Нужна помощь с проблемой.
Цель
Я собираю приложение для iOS, которое использует NSTimers для запуска нескольких событий в шахматном порядке после загрузки представления. Я создал класс MethodCallerWithTimer , чтобы помочь мне сделать это (код внизу).
Мое решение до сих пор
Когда я использую класс MethodCallerWithTimer , я назначаю objectOwningMethod как мой объект подкласса UIViewController (это страница книги), а затем метод как метод экземпляра в этом классе. Вот пример метода, который я назначаю - довольно просто включить некоторые изображения на экране:
- (void) playEmory {
[emoryRedArt setHidden:NO];
}
Мой выпуск
Когда я создаю несколько экземпляров MethodCallerWithTimer , затем загружаю представление и запускаю их все, я получаю только событие FIRST. Ни один из других таймеров не вызывает свои целевые методы. Я подозреваю, что не понимаю, что я прошу сделать NSRunLoop или что-то подобное.
Есть мысли?
Вот мой MethodCallerWithTimer класс:
@interface MethodCallerWithTimer : NSObject {
NSTimer * timer;
NSInvocation * methodInvocationObject;
NSNumber * timeLengthInMS;
}
- (id) initWithObject: (id) objectOwningMethod AndMethodToCall: (SEL) method;
- (void) setTime: (int) milliseconds;
- (void) startTimer;
- (void) cancelTimer;
@end
И реализация:
#import "MethodCallerWithTimer.h"
@implementation MethodCallerWithTimer
- (id) initWithObject: (id) objectOwningMethod AndMethodToCall: (SEL) method {
NSMethodSignature * methSig = [[objectOwningMethod class] instanceMethodSignatureForSelector:method];
methodInvocationObject = [NSInvocation invocationWithMethodSignature:methSig];
[methodInvocationObject setTarget:objectOwningMethod];
[methodInvocationObject setSelector:method];
[methSig release];
return [super init];
}
- (void) setTime: (int) milliseconds {
timeLengthInMS = [[NSNumber alloc] initWithInt:milliseconds];
}
- (void) startTimer {
timer = [NSTimer scheduledTimerWithTimeInterval:([timeLengthInMS longValue]*0.001) invocation:methodInvocationObject repeats:NO];
}
- (void) cancelTimer {
[timer invalidate];
}
-(void) dealloc {
[timer release];
[methodInvocationObject release];
[timeLengthInMS release];
[super dealloc];
}
@end