简体   繁体   English

在导航控制器中一次添加通用视图

[英]Add common views once in navigation controller

What I'm trying to achieve is to design my "shared" part of UI in interface builder and use child view controllers to show content to the user. 我要实现的目标是在界面构建器中设计UI的“共享”部分,并使用子视图控制器向用户显示内容。 This may sound as trying to reinvent UINavigationController , but it is not. 这听起来像是在尝试重塑UINavigationController ,但事实并非如此。 In fact, the whole thing is embedded in one. 实际上,整个事物都嵌入其中。

It looks like this : 看起来像这样: 示例视图

Now, what I'm trying to do is change child view controllers of this BaseViewController and indicate this change in the navigation bar, so that all of its functionality remains. 现在,我想做的是更改此BaseViewController子视图控制器,并在导航栏中指示此更改,以便保留其所有功能。

I tried adding such a method : 我尝试添加这样的方法:

+ (UIViewController *)baseViewControllerWithChild:(UIViewController *)child {
    BaseViewController *base = [BaseViewController new];
    [base addChildViewController:child];
    child.view.frame = base.childViewControllerContainer.frame;
    [base.view addSubview:child.view];
    [child didMoveToParentViewController:base];

    return base;
}

and then using it like this : 然后像这样使用它:

- (void)didTouch:(UIButton *)sender { 
    [self.parentViewController.navigationController pushViewController:[BaseViewController baseViewControllerWithChild:[DummyViewController new]] animated:YES];   
}

(Note : DummyViewController is exactly that - a dummy vc, made just for testing, it only has background color set in viewDidLoad ) (注意: DummyViewController就是这样-一个虚拟vc,仅用于测试,它仅在viewDidLoad设置了背景色)

This method is a handler of a button in first child view controller. 此方法是第一个子视图控制器中按钮的处理程序。 So far so good. 到现在为止还挺好。 Unfortunately, the result is not as expected - the pushed view controller is black. 不幸的是,结果不符合预期-推送的视图控制器为黑色。 At firs I thought this was because BaseViewController was designed in storyboard and initially set as rootViewController of navigation controller. 首先,我认为这是因为BaseViewController是在storyboard rootViewController中设计的,最初设置为导航控制器的rootViewController Moving it to a xib file and setting from code didn't quite work for me, as you cannot add a Container View in a xib . 将其移动到xib文件并从代码进行设置对我而言并不起作用,因为您无法在xib添加Container View

To summarise, I would like to have a base design governed by BaseViewController class and content would be added as a childViewController of it. 总而言之,我想有一个受BaseViewController类控制的BaseViewController设计,内容将作为它的childViewController添加。 Pushing a new view controller would be a result of an action on these childViewController and should update the navigation stack accordingly. 推送新的视图控制器将是对这些childViewController进行操作的结果,并且应相应地更新导航堆栈。

Also, the whole thing needs to work with iOS 7. 另外,整个事情都需要与iOS 7一起使用。

Any help as to how to try to achieve this is greatly appreciated! 非常感谢您对如何实现这一目标的任何帮助!

The issue was casued by base.childViewControllerContainer being nil - this was caused byt he fact that view property of view controllers is loaded lazily. 该问题是由于base.childViewControllerContainer nil引起的-这是由于视图控制器的view属性延迟加载的事实引起的。 Adding [base view] before accesing base.childViewControllerContainer solved the issue, though I'm not sure if this is the one, only and best way to do this. 在访问base.childViewControllerContainer之前添加[base view]解决了该问题,尽管我不确定这是否是唯一,最好的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM