2011-11-08 4 views
0

임 NScimer에 대해 배우고 문서를 읽은 후 간단한 테스트를했습니다. 나의 이해는 viewDidLoad를 main에서 호출 할 때 viewDidLoad는 3.0 초 후에 runTest를 호출해야하지만 작동하지 않는다는 것입니다. 그것은 오류없이 잘 컴파일하지만 runTest (NSLog 없음)로드하지 않습니다 도와주세요 ... 고마워요.간단한 NSTimer 테스트가 작동하지 않습니다.

#import "MyClass.h" 
#import <Foundation/Foundation.h> 

int main (int argc, const char * argv[]) 
{ 

NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; 

MyClass *trial = [[MyClass alloc]init]; 

[trial viewDidLoad]; ///// this should call viewDidLoad ///// 

[pool drain]; 
return 0; 
} 



#import <Foundation/Foundation.h> 

@interface MyClass : NSObject { 

NSTimer *aTimer;} 

@property (nonatomic, retain) NSTimer *aTimer; 

- (void)viewDidLoad; 

- (void)runTest; 

@end 





#import "MyClass.h" 

@implementation MyClass 

@synthesize aTimer; 

- (void)viewDidLoad { 
    aTimer = [NSTimer scheduledTimerWithTimeInterval:3.0 target:self  selector:@selector(runTest) userInfo:nil repeats:NO];} 

- (void) runTest { 
NSLog(@"Working - runTest has loaded !"); 
aTimer = nil; 
} 
@end 
+0

viewDidLoad를 호출 하시겠습니까? – jbat100

답변

2

이렇게하면 작동하지 않습니다. NSTimer에는 runloop이 필요합니다.이 런 루프는 일반 코코아 응용 프로그램을 Foundation 명령 줄 도구로 만들지 않을 때 자동으로 만들어집니다.

응용 프로그램 대리인의 applicationDidFinishLaunching 메서드 또는 타이머의 인스턴스를 주 펜촉에서 인스턴스화하면 개체의 init에 만듭니다.

+0

NSTimer는 콘솔 전용으로 작은 앱을 제작할 때 어떤 형태로도 사용할 수 없다는 말입니까? 그렇다면 applicationDidFinishLaunching의 작은 예가 좋습니다. – pete

+0

새로운 코코아 애플리케이션을 만들고 AppDelegate 클래스로 이동하십시오. '- (void) applicationDidFinishLaunching : (NSNotification *) aNotification' 메소드가 보일 것입니다. – Davyd

관련 문제