简体   繁体   中英

Draw in rect not getting called

So I have a chart drawing view. It is in a view controller that is presented as modal. The problem is that drawInRect doesn't get called. Why is that? The view is set up in Interface Builder, and initialized ...

- (void)viewWillAppear:(BOOL)animated
{
    minVal = [self.delegate minValue];
    maxVal = [self.delegate maxValue];
    noSteps = [self.delegate noSteps];
    stepVal = [self.delegate stepValue];
    data = [self.delegate dataArray];
    formulaName = [self.delegate formulaName];
    self.titleLabel.text = formulaName;

    // draw the chart
    self.chartView = [[PSChartIOS alloc] initWithFrame:self.chartView.bounds andChartType:PSChartTypeColumn];
    [self.chartView setDataSource:self];
    [self.chartView setChartTitle:@""];
    [self.chartView setPanOXAxis:YES];
    [self.chartView setTitleColor:[UIColor redColor]];
    [self.chartView setStartAtTheEnd:NO];
    //[chart setAutoresizingMask: UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth];

    [self.chartView loadData];
    [self.chartView setNeedsDisplay];
    // NSLog(@"autoresize mask :%d",self.view.autoresizingMask);

//    [self.view addSubview:self.chartView];
//    [self.chartView addSubview:chart];




    [super viewWillAppear:animated];
}

All the delegate methods are called, so the view has it's data... it never draws... I have a felling that the problem might be self.chartView.bounds .. but that should just return the bounds is set up in IB...

Found the problem :

 NSLog(@"the bounds %@",NSStringFromCGRect( self.chartView.bounds));

returns

the bounds {{0, 0}, {0, 0}}

I thought that it will get the bounds from IB .. apparently not

You have commented out the line that puts the PSChartIOS view into the view hierarchy.

//    [self.view addSubview:self.chartView];

I would bet that once you did that everything will be good.

Also you frame doesn't make sense. Create the view with a frame like

CGRectMake(0,0,200,200).  

This will test to make sure you can see a Chart at 200x200 in the view controller's view.

I can't see drawInRect method call in your code.

If you are expecting setNeedsDisplay to call it. Then it can't be called automatically. If you have created your own method then you need to call it explicitly.

However it will call drawRect .

EDIT:

You did a typo there is no method called drawInRect: it is drawRect:

- (void)drawRect:(CGRect)rect {
    // Drawing code
    // only draws what is specified by the rect parameter
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM