2010-12-20 89 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