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

发布于 2024-10-08 13:51:17 字数 467 浏览 1 评论 0原文

我想通过单击按钮添加 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];    
} 

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

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 following 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

红尘作伴 2024-10-15 13:51:17

创建对象后,永远不应该对其调用 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];    
}

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];    
}
初见你 2024-10-15 13:51:17

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

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).

柠檬 2024-10-15 13:51:17

您可以尝试如下:

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

}

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

}
风吹雨成花 2024-10-15 13:51:17

我建议你尝试这个:

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

I suggest you try this:

[joinmeetup setFrame:CGRectMake(0, 108,320,351)];
[self.view addSubview:joinmeetup];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文