2010-05-29 94 views
0

我有從UIViewController繼承ClockViewController.h和ClockViewController.m。
另外2個文件ClockView.h和ClockView.m繼承自UIView。在界面生成器中,我爲時鐘選擇了類「ClockView」,但是我的drawRect沒有執行。我通過定時器函數的setNeedsDisplay調用它。即使定時器功能沒有調用。
這裏是代碼
ClockViewController.m
drawRect沒有執行

import "ClockViewController.h" 

@implementation ClockViewController 
- (void)didReceiveMemoryWarning { 
    // Releases the view if it doesn't have a superview. 
    [super didReceiveMemoryWarning]; 

    // Release any cached data, images, etc that aren't in use. 
} 

- (void)viewDidUnload { 
    // Release any retained subviews of the main view. 
    // e.g. self.myOutlet = nil; 
    [super viewDidUnload]; 
} 

- (void)dealloc { 
    [super dealloc]; 
} 
@end 

這裏是ClockView.m

import "ClockView.h" 

@implementation ClockView 

-(id)initWithCoder:(NSCoder *)coder{ 
    timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(clockTick) userInfo:nil repeats:YES]; 

    return self; 
} 

-(id)initWithFrame: (CGRect)frame{ 
    if(self = [super initWithFrame: frame]){ 
     //Initialization code 
    } 
    return self; 
} 

-(void) drawRect: (CGRect)rect{ 
    NSLog(@"from Rect"); 
} 

-(void)clockTick{ 
    NSLog(@"test"); 
} 

-(void)dealloc{ 
    [super dealloc]; 
} 
@end 

+0

任何代碼? _____ – kennytm 2010-05-29 08:37:37

回答

1

-initWithCoder:必須調用[super initWithCoder:coder]

另外,爲了一致性,既-initWithCoder:-initWithFrame:應該調用安裝定時器的代碼。

+0

仍然無效。我已經調用了[super initWithCode:coder,並且還在initWithFrame方法中添加了timer = ...。 – coure2011 2010-05-29 11:47:26

+0

感謝它的工作。再次從頭創建項目並應用您的答案。 – coure2011 2010-05-29 12:35:46