开发者

drawRect is not executing

开发者 https://www.devze.com 2023-01-01 17:28 出处:网络
I have ClockViewController.h and ClockViewController.m inherited from UIViewController. Also 2 other files ClockView.h and ClockView.m inherited from UIView. In Interface builder i have selected Class

I have ClockViewController.h and ClockViewController.m inherited from UIViewController.

Also 2 other files ClockView.h and ClockView.m inherited from UIView. In Interface builder i have selected Class "ClockView" for the clock, but my drawRect is not executing. I am calling it via setNeedsDisplay from a timer function. even the timer function is not calling.

here is the code

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

Here is 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
开发者_如何学Go


Your -initWithCoder: have to call [super initWithCoder:coder].

Also, for consistency, both -initWithCoder: and -initWithFrame: should call the code that installs the timer.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号