我在學習有關NSTimer和讀完文檔後,我做了這個簡單的測試。我的理解是,當我在main中調用viewDidLoad時,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
你確定你在調用viewDidLoad嗎? – jbat100