将从笔尖加载的子视图添加到框架中

时间:2010-12-20 06:00:49

标签: iphone uiview

我想通过点击按钮将nib文件中的视图添加为主视图的子视图,但它必须位于框架中(0,108,320,351)。我尝试过以下代码:

-(IBAction) displayJoinmeetup:(id)sender  
{  
  [meetups removeFromSuperview]; //removing the older view   
  CGRect myFrame = CGRectMake(0, 108,320,351);  
  [joinmeetup initWithFrame:myFrame];  //joinmeetup declared in header file as IBoutlet UIView*joinmeetup     
  [self.view addSubview:joinmeetup];    
} 

这只显示一个空白屏幕,否则如果我删除了子视图显示覆盖整个屏幕的帧,我该如何正确地执行此操作?

4 个答案:

答案 0 :(得分:2)

创建对象后,永远不应该在对象上调用init ....如果您可以更改设置,则会有一个设置器。在这种情况下,它是setFrame:

-(IBAction) displayJoinmeetup:(id)sender {  
    [meetups removeFromSuperview]; //removing the older view   
    CGRect myFrame = CGRectMake(0, 108,320,351);  
    [joinmeetup setFrame:myFrame];  //joinmeetup declared in header file as IBoutlet UIView*joinmeetup     
    [self.view addSubview:joinmeetup];    
}

答案 1 :(得分:0)

没有为从nib加载的视图调用initWithFrame。有关详细信息,请参阅UIView类ref。 您需要在界面构建器(检查器)中设置视图大小属性。

答案 2 :(得分:0)

你可以尝试如下:

-(IBAction) displayJoinmeetup:(id)sender
{
if(meetups)
{
[meetups removeFromSuperview]; //removing the older view
//also here you should release the meetups, if it is allocated
meetups = nil;
}

UIView *tempView = [[UIView alloc] initWithFrame:CGRectMake(0, 108,320,351)];
joinmeetup = tempView;
[tempView release];
[self.view addSubview:joinmeetup];

}

答案 3 :(得分:0)

我建议你试试这个:

[joinmeetup setFrame:CGRectMake(0, 108,320,351)];
[self.view addSubview:joinmeetup];