0

我正在上一门编程课(针对新手),我需要以UIViewController graphViewController编程方式创建 's 视图(没有界面生成器)。

视图很简单,它只包含一个名为IBOUtlet的子类的实例。响应几个用于缩放和平移等的多点触控手势,但我在.UIViewGraphViewgraphView- (void)viewDidLoad

我只是在创建self.view属性并graphView在下面的代码中:

- (void)loadView
{
    UIView *gvcView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
    self.view = gvcView;
    [gvcView release];

    GraphView *aGraph = [[GraphView alloc] initWithFrame:self.view.bounds];
    self.graphView = aGraph;
    [aGraph release];

}

当我运行应用程序时,我看不到graphView那里的视图。我只是得到一个显示“我的通用应用程序”标签的透明视图。我难住了。请帮忙。

如果您需要其他代码,请告诉我。

谢谢!

更新:非常感谢 BJ Homer 的快速修复!

必须执行以下操作:

添加这行代码:[self.view addSubview:self.graphView];在最后。

我也遇到了这个奇怪的错误,graphView它显示为完全黑色。这行代码修复了:self.graphView.backgroundColor = [UIColor whiteColor];

就是这样!

最后一个问题:自定义 UIView 的默认背景颜色是黑色吗?

再次感谢!

4

1 回答 1

1
[self.view addSubview:self.graphView];

Until you add your graph view to a parent view, UIKit doesn't know where to display it. self.view is special, since that's the property that -loadView is supposed to set. That view will automatically be added to the screen. But your graph view is just floating off in the ether until you add it somewhere.

于 2011-09-09T04:36:40.373 回答