drawRect没有执行

时间:2010-05-29 08:35:21

标签: objective-c iphone

我有从WinIViewController继承的ClockViewController.h和ClockViewController.m 还有2个其他文件ClockView.h和ClockView.m继承自UIView。在Interface builder中,我为时钟选择了Class“ClockView”,但我的drawRect没有执行。我通过setNeedsDisplay从计时器函数调用它。甚至定时器功能也没有调用 这是代码
ClockViewController.m

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

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

1 个答案:

答案 0 :(得分:1)

您的-initWithCoder:必须致电[super initWithCoder:coder]

此外,为保持一致性,-initWithCoder:-initWithFrame:都应调用安装计时器的代码。