【问题标题】:Add UIViewControllers View as a subView of another UIViewController view添加 UIViewControllers 视图作为另一个 UIViewController 视图的子视图
【发布时间】:2013-04-08 09:47:23
【问题描述】:

我有一个名为 TestView 的 UiView,我已将其添加为 NumberViewController 的子视图。

在我的应用程序启动时,我的 rootViewController 是 PaperViewController,这有一些我通过 xib 添加的 UIButtons,现在我想将 NumberViewController 的视图添加为 PaperViewController 的子视图,所以我这样做了

 NumberViewController = [[NumberViewController alloc] initWithNibName:@"NumberViewController" bundle:nil];
    NumberViewController.delegate = self;
    [self.view addSubview:NumberViewController.view];
    NumberViewController.view.backgroundColor = [UIColor clearColor];
    NumberViewController.view.hidden = YES;

现在这是隐藏的,因为我会在单击某个按钮时取消隐藏它,然后我会显示 NumberViewContrller 的视图。这一切都很好。

但是假设我通过代码在 PaperViewController 中添加了一些按钮,然后取消隐藏 NumberViewController 的视图,那么两个视图重叠了。

我不明白问题出在哪里,我犯了什么错误。所以请朋友们帮帮我

问候 兰吉特。

【问题讨论】:

  • 为什么要将第二个视图控制器视图添加为子视图而不是呈现第二个视图控制器?
  • @jcesar bcoz,我想立即显示,而不是 modalView。
  • 您能否发布您的代码如何以编程方式添加按钮并取消隐藏视图?
  • 对不起,你的问题我不清楚,你到底想达到什么目的?
  • @Jennis,以编程方式添加按钮的代码是标准代码。

标签: ios ios5 ios6 uiviewcontroller addsubview


【解决方案1】:

在Header中添加UIView:

@property (weak, nonatomic) IBOutlet UIView *addChildView;

- (void)viewDidLoad - 方法这样做:

  ChildViewController *nonSystemsController = [[ChildViewController alloc] initWithNibName:@"ChildViewController" bundle:nil];

    nonSystemsController.view.frame = self.addChildView.bounds;    
    [self.addChildView addSubview:nonSystemsController.view];

    [nonSystemsController didMoveToParentViewController:self];
[self addChildViewController:nonSystemsController];

【讨论】:

    猜你喜欢
    • 2012-12-06
    • 2010-12-01
    • 2013-04-21
    • 1970-01-01
    • 2012-04-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多