容器视图

时间:2016-02-29 16:34:49

标签: objective-c xcode uicontainerview childviewcontroller

我试图让我的头围绕子视图控制器。 我用一个主视图控制器做了一个简单的项目。

步骤1:在该视图控制器中,我放置了一个应该显示子视图的UIButton。

enter image description here

步骤2:我添加了一个容器视图,并为可见性添加了一个红色背景,以及一个稍后将其关闭的按钮。

enter image description here

我运行该项目以查看到目前为止的样子。

enter image description here

现在,我遇到的第一个问题是我不希望从开始时看到子视图,我希望它在我按下绿色按钮时出现。所以我知道我必须以某种方式隐藏它。我到目前为止看到的两个选项是

self.vc.view.hidden = YES;

self.vc.view.alpha = 0;

但我的问题是我不完全理解如何访问这个子视图控制器来做任何一个。我是否正确地思考这些问题:

用于父视图控制器的文件:

#import <UIKit/UIKit.h>
#import "ChildViewController.h"

@interface ViewController : UIViewController

@property ChildViewController *vc;

- (IBAction)CreateChildVC:(id)sender;

@end

m file:

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.vc = [[ChildViewController alloc]init];
    [self addChildViewController:self.vc];
    [self.view addSubview:self.vc.view];
    self.vc.view.hidden = YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)GoToView2:(id)sender {

}

@end

这不起作用,因为当我运行它时,子视图是可见的。我尝试通过以下方式将像颜色背景这样激烈的东西改为黑色:

self.vc.view.backgroundColor = [UIColor blackColor];

并且它不会以任何方式影响子视图控制器。我是否必须使用

之类的东西
   self.vc = [self.storyboard instantiateViewControllerWithIdentifier:@"ChildVC"];

有很多选择,很容易迷路。任何帮助非常感谢。

0 个答案:

没有答案
相关问题