简体   繁体   English

使用情节提要板时,使用UITabBarController在视图之间传递数据

[英]Passing data between views with a UITabBarController when using storyboards

I've been converting an application to use storyboards. 我一直在将应用程序转换为使用情节提要。 I'm sure this is a simple problem, but somehow I can't seem to figure out the 'correct' way of doing it, coming as we are from the old XIB world. 我确信这是一个简单的问题,但是由于我们来自旧的XIB世界,所以我似乎无法以某种方式找出解决问题的“正确”方法。

One of the subsections of it contains a UITabBarController, each with some subviews within it. 它的一个小节包含一个UITabBarController,每个小节中都有一些子视图。

The action that launches this set of tabs works perfectly; 启动这组选项卡的操作非常有效; I detect the segue, and set some data properties within my (custom) UITabBarController. 我检测到segue,并在我的(自定义)UITabBarController中设置了一些数据属性。

Next, I would like to be able to pass that data to the child views when they get created. 接下来,我希望能够在创建子视图时将该数据传递给子视图。 But - because these tabs are simply 'relationships' and not segue's, I can't do what I do everywhere else, which is override the 'prepareForSegue' function. 但是-因为这些标签只是“关系”而不是segue的标签,所以我无法做我在其他任何地方所做的事情,而这是覆盖“ prepareForSegue”功能的。

In the old XIB universe, I'd simply bind some IBOutlets together between the tab controller and the child views. 在旧的XIB世界中,我只是将一些IBOutlet绑定在选项卡控制器和子视图之间。 But I can't do that in storyboards, because the parent and children are separate 'scenes'. 但是我无法在情节提要中做到这一点,因为父母和孩子是分开的“场景”。

I've tried making my UITabBarController class implement its own delegate, override 'didSelectViewController' and doing 'self.delegate = self' which almost works, except for the fact that it is never called with the first tab when the view is initially shown. 我试过让我的UITabBarController类实现自己的委托,重写'didSelectViewController'并执行'self.delegate = self',这几乎可以工作,除了在最初显示视图时从未在第一个选项卡中调用它。

What's the "correct" (or 'best') way to do this? 什么是“正确”(或“最佳”)方法? Please don't tell me to get/set some value on the app delegate, as this is 'global variable' territory - nasty. 请不要告诉我在应用程序委托上获取/设置一些值,因为这是“全局变量”领域-讨厌。

Try looping through the view controllers on the UITabBarController, eg in this example the setData method is called from the segue in to the UITabBarController, and it then loops through the child view controllers, making a similar call on the child controller to set the data on that too; 尝试循环遍历UITabBarController上的视图控制器,例如,在此示例中,从segue到UITabBarController调用setData方法,然后循环遍历子视图控制器,在子控制器上进行类似的调用以设置数据那个也是;

- (void)setData:(MyDataClass *)newData
{
    if (_myData != newData) {
        _myData = newData;

        // Update the view.
        [self configureView];
    }

}

- (void) configureView {
       for (UIViewController *v in self.viewControllers)
       {
           if ([v isKindOfClass:[MyDetailViewController class]])
           {
               MyDetailViewController *myViewController = v;
                    [myViewController setData:myData];
           }
       }
 }

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

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