2010-12-20 103 views
0

我想在点击按钮时从笔尖文件添加视图作为我的主视图的子视图,但它必须位于框架中(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];  
} 

此只显示黑屏,否则如果我删除帧子视图显示覆盖了整个屏幕,我怎么能正确地做到这一点?

回答

2

已创建后,您应该调用初始化...的对象上。如果您可以更改设置,则会有一个设置。在这种情况下,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];  
} 
+0

谢谢你现在正常工作! – sujith1406 2010-12-20 06:25:01

0

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

0

您可以尝试THI如下:

-(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]; 

} 
0

我建议你试试这个:

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

框架是在UIView中声明的属性 – 2010-12-20 06:17:09