2012-11-08 54 views
1

我有一个绿色的矩形,但它不会显示当我运行该应用程序。 这里是我的代码:如何将子视图添加到视图控制器?

- (void) makeBlockg { 

    x = arc4random()%(320) + 1; 

    CGRect block = CGRectMake(x, 0, 100, 70); 

    greenblock.frame = block; 

    greenblock.self.backgroundColor = [UIColor greenColor]; 

    [self.view addSubview:greenblock]; 

} 
+3

不应该是'greenblock.backgroundColor'吗? –

+1

尝试'greenblock = [[UIView alloc] initWithFrame:block]',以及Nathan说什么 – yeesterbunny

+0

你在哪里分配greenblock? – mayuur

回答

0

此代码工作正常,我。你确定你用greenblock = [[UIView alloc] init]正确地初始化了greenblock吗?没有做这个greenblock是零,而不是一个空的UIView对象。

+0

非常感谢你!我一直在为此工作一段时间...我想我只是忘了分配它。 – user1807869

相关问题