gpt4 book ai didi

ios - UIView addSubview subview 不显示

转载 作者:可可西里 更新时间:2023-11-01 03:27:29 26 4
gpt4 key购买 nike

- (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:

@interface HeadViewController : UIViewController
{
IBOutlet UIView *view;
}
@property (nonatomic, retain)IBOutlet UIView *view;
@end

然后我将 View 连接到文件的所有者。

而且我看不到 headViewController.view

最佳答案

首先,您不需要在HeadViewController 类中定义view outlet。它自动继承自 UIViewController 父类(super class)。

那么,我建议您直接将 HeadViewController 的 View 添加到您当前的 View 中。例如。

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

但是,如果您使用的是 ARC(自动引用计数),headViewController 实例可能会在 viewDidLoad 方法结束后被释放。将该实例分配给您当前显示的 Controller 中的局部变量很方便(我会说这是强制性的)。这样您以后就可以在需要时处理它的 View 组件,实例将被保留,其他一切都将完美运行。你应该有这样的东西:

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

@interface MyController ()
@property (nonatomic, strong) HeadViewController *headViewController;
@end

.m类实现文件开头的隐藏接口(interface)定义中。

关于ios - UIView addSubview subview 不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9920146/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com