开发者

add subview loaded from a nib into a frame

开发者 https://www.devze.com 2023-01-31 04:45 出处:网络
I want to add a view from a nib file as a subview of my main view at the click of a button, but it has to be in a frame (0,108,320,351). I\'ve tried the fo开发者_Python百科llowing code:

I want to add a view from a nib file as a subview of my main view at the click of a button, but it has to be in a frame (0,108,320,351). I've tried the fo开发者_Python百科llowing code:

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

This displays only a blank screen, otherwise if I remove the frame the subview displays covering the whole screen, how can I properly do this?


You should never call init... on an object after it has been created. If you can change a setting, there will be a setter for it. In this case, it is 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];    
}


initWithFrame is not called for a view loaded from nib. See the UIView class ref for details. You need to set the view size properties in interface builder (inspector).


You can try thi as follow:

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

}


I suggest you try this:

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

精彩评论

暂无评论...
验证码 换一张
取 消