2010-05-29 90 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