故事板:如何链接到多个容器的观点一个观点? [英] Storyboard: How to link a single view to multiple container views?

查看:120
本文介绍了故事板:如何链接到多个容器的观点一个观点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的宗旨:

我是用故事创造我的应用程序的意见

I am using storyboard to create the views in my App.

我的目标是有将暴露,并在应用程序的所有页面链接视图的一个实例。在如果有一个应用程序有许多标签我的情况,我想该视图将出现在所有选项卡,并具有相同的状态。

My aim is to have a single instance of a view that would be exposed and linked in all pages of the App. In my case if there is an App with many tabs, I want that the view would appear in all tabs and have the same state.

我的尝试和做即:

我创建的视图,并在那些在不同标签的容器意见嵌入它

I created a view and embedded it in container views that are in the different tabs.

在打开的看法似乎和作品,在所有页面上的应用程序。但是,如果我在一个视图改变它不会传播到其他视图。

When opening the app the view appears and works, on all pages. However, if I make a change in one view it doesn't propagate to the other views.

在换句话说,在下面的例子中,如果我改变一页的开关的状态,这种变化将不会出现,当我导航到其它页面(在我应用程序我有标签来做到这一点)。

In other words, in the example below if I change the state of the switch in one page, that change would not appear when I navigate to the other page (in my app I have tabs to do that).

我看到有处于性状链接选项,但没有帮助。

I saw that there is a "link" option under "Traits" but that didn't help.

我怎样才能确保只有一个在所有选项卡视图的瞬间?

How can I make sure there is only one instant of the view in all tabs?

干杯!

推荐答案

正如我所说的在我的意见,你不能塞格斯做到这一点,因为他们总是创建新实例。所以,我认为你将不得不在代码中添加子控制器到每个需要它的控制器。在故事板,可以将子视图添加到每个有需求的嵌入式控制器充当一个占位符控制器。给人以开关,一个不规则形状的大小控制,其大小设置为相同的大小,您添加到其他控制器的子视图,并取消选中,说:从笔尖调整视图对话框。下面是一个例子故事板,

As I said in my comment, you can't do this with segues, since they always create new instances. So, I think you will have to add the child controller in code to each controller that needs it. In the storyboard, you can add a subview to each controller that needs the embedded controller that acts as a placeholder. Give the controller with the switch, a freeform size, and set its size to the same size as the subviews you added to the other controllers, and uncheck the box that says "Resize View from NIB". Here is an example storyboard,

在代码中,你需要在viewDidAppear添加控制器,并在viewDidDisappear删除它(这样它可以被添加到下一个控制器 - 它可以' T为在两个地方一次)。在控制器中,您将创建与开关控制器的一个实例,而在所有其他控制器你到同一实例的引用。因此,第一个控制器,

In code, you'll need to add the controller in viewDidAppear, and remove it in viewDidDisappear (so it can be added to the next controller -- it can't be in two places at once). In the controller, you'll create an instance of the controller with the switch, and in all other controllers you get a reference to that same instance. So, in the first controller,

- (void)viewDidLoad {
    [super viewDidLoad];
    self.embed = [self.storyboard instantiateViewControllerWithIdentifier:@"SharedVC"];
}

-(void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    [self addChildViewController:self.embed];
    [self.container addSubview:self.embed.view];
}

-(void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
    [self.embed.view removeFromSuperview];
    [self.embed willMoveToParentViewController:nil];
    [self.embed removeFromParentViewController];
}

在所有其他控制器,你需要像这样在viewDidAppear(viewDidDisappear会是一样的,在第一控制器),

In all the other controllers, you need something like this in viewDidAppear (viewDidDisappear would be the same as in the first controller),

-(void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    self.embed = [(FirstViewController *)self.tabBarController.viewControllers[0] embed];
    [self addChildViewController:self.embed];
    [self.container addSubview:self.embed.view];
}

这篇关于故事板:如何链接到多个容器的观点一个观点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆