ios UIView addSubview 并且不显示子视图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9920146/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
UIView addSubview and the subview isn't displayed
提问by jxdwinter
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
HeadViewController *headViewController = [[HeadViewController alloc] initWithNibName:@"HeadViewController" bundle:nil];
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 120)];
[view addSubview:headViewController.vew];
[self.view addSubview:view];
}
HeadViewController.h:
HeadViewController.h:
@interface HeadViewController : UIViewController
{
IBOutlet UIView *view;
}
@property (nonatomic, retain)IBOutlet UIView *view;
@end
and I connect the view to the file's owner.
我将视图连接到文件的所有者。
And I can't see the headViewController.view
.
而且我看不到headViewController.view
.
回答by marzapower
First of all, you do not need to define the view
outlet in the HeadViewController
class. It is automatically inherited from the UIViewController
super class.
首先,您不需要view
在HeadViewController
类中定义插座。它自动从UIViewController
超类继承。
Then, I suggest you to add directly the view of HeadViewController
to your current view. Eg.
然后,我建议您直接将 的视图添加HeadViewController
到您当前的视图中。例如。
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
HeadViewController *headViewController = [[HeadViewController alloc] initWithNibName:@"HeadViewController" bundle:nil];
headViewController.view.frame = CGRectMake(0, 0, 320, 120);
[self.view addSubview:headViewController.view];
}
But, if you are using ARC (Automatic Reference Counting), the headViewController
instance will probably be deallocated after the end of the viewDidLoad
method. It is convenient (and I'd say it is compulsory) to assign that instance to a local variable in the controller you are currently displaying. This way you will be able to handle its view's components later if needed, the instance will be retained, and everything else will work perfectly. You should have something like:
但是,如果您使用的是 ARC(自动引用计数),则该headViewController
实例可能会在viewDidLoad
方法结束后被释放。将该实例分配给您当前显示的控制器中的局部变量很方便(我认为这是强制性的)。通过这种方式,您可以稍后在需要时处理其视图的组件,该实例将被保留,其他一切都将完美运行。你应该有类似的东西:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
self.headViewController = [[HeadViewController alloc] initWithNibName:@"HeadViewController" bundle:nil];
headViewController.view.frame = CGRectMake(0, 0, 320, 120);
[self.view addSubview:headViewController.view];
}
and
和
@interface MyController ()
@property (nonatomic, strong) HeadViewController *headViewController;
@end
in the hidden interface definition at the beginning of the .m
class implementation file.
在.m
类实现文件开头的隐藏接口定义中。
回答by Daniel Ford
It looks like a typo - forgot the i in .view
它看起来像一个错字 - 忘记了 .view 中的 i
[view addSubview:headViewController.vew];
[查看 addSubview:headViewController.vew];
回答by Bharath
i is missing in the syntax
我在语法中丢失
[view addSubview:headViewController.view];
[查看 addSubview:headViewController.view];