如何实现loadView?

时间:2011-03-14 01:55:48

标签: iphone objective-c cocoa-touch ios loadview

我创建了一个名为GraphView的自定义视图。加载视图时,我得到的只是一个空白的黑屏。这是我的代码:

GraphViewController.m中的

@synthesize graphView, graphModel;

- (void)loadView
{   
    GraphView *aGraphView = [[GraphView alloc] initWithFrame:CGRectZero];
    self.view = aGraphView;
    self.graphView = aGraphView;

    [aGraphView release];
}

当我尝试在loadView

中实施GraphViewController.m时,我不确定为什么会出现黑屏

2 个答案:

答案 0 :(得分:2)

您没有为GraphView对象设置框架:

GraphView *aGraphView = [[GraphView alloc] init];

UIView的指定初始值设定项为-initWithFrame:。做这样的事情(根据需要设置视图的大小/原点):

GraphView *aGraphView = [[GraphView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];

答案 1 :(得分:2)

我需要在loadView

中将背景颜色设为白色
- (void)loadView
{   
    GraphView *aGraphView = [[GraphView alloc] initWithFrame:CGRectZero];
    aGraphView.backgroundColor = [UIColor whiteColor];
    self.view = aGraphView;
    self.graphView = aGraphView;

    [aGraphView release];
}