ios – UIView addSubview并且不显示子视图

前端之家收集整理的这篇文章主要介绍了ios – UIView addSubview并且不显示子视图前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
- (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,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

然后我将视图连接到文件的所有者.

而且我看不到headViewController.view.

解决方法

首先,您不需要在HeadViewController类中定义视图插座.它自动从UIViewController超类继承.

然后,我建议您直接将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,120);
     [self.view addSubview:headViewController.view];
}

但是,如果您使用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,120);
     [self.view addSubview:headViewController.view];
}

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

在.m类实现文件开头的隐藏接口定义中.

猜你在找的iOS相关文章