ios 将数据从 TabbarController 传递到 Storyboard 中的 ViewController [英] ios Pass data from TabbarController to ViewController in Storyboard

查看:22
本文介绍了ios 将数据从 TabbarController 传递到 Storyboard 中的 ViewController的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 TabBarController,在 storboard 编辑器中设置了 4 个选项卡.当我选择第四个选项卡时,我想向其视图控制器发送一个字符串.在我的 TabbarController 类(名为 TabBarVC)中,我实现了以下方法:

I have a TabBarController with 4 tabs setup in storboard editor. When I select the 4th tab, I want to send a string to its view controller. In my TabbarController class (named TabBarVC) I implemented the following method:

- (void)tabBarController:(UITabBarController *)theTabBarController didSelectViewController:(UIViewController *)viewController {
    NSUInteger indexOfTab = [theTabBarController.viewControllers indexOfObject:viewController];
    if(indexOfTab == 3) {
        SupplierListVC *slvc = (SupplierListVC *) viewController;
        slvc.locationType = @"favorite";
        self.tabBarController.selectedViewController = slvc;
    }
}

这个方法被调用得很好,但字符串没有被传递.我调试的时候发现上面这段代码是在SupplierListVC的viewDidLoad之后调用的.我在这里做错了什么?选择选项卡时如何传递字符串?

This method is being called just fine but the string is not getting passed. When I debugged, I noticed that the above piece of code is called after viewDidLoad of SupplierListVC. What am I doing wrong here? How can I pass the string when I select the tab?

推荐答案

如你所说,这个方法在viewDidLoad之后调用.方法名称本身应该告诉你:**DID**SelectViewController

As you said, this method is called after viewDidLoad. The method name itself should tell you that: **DID**SelectViewController

过去时.英语可能不是您的母语,但是在使用 Apple 的方法名称时,对英语动词时态的良好理解真的非常非常非常非常非常长的路要走.他们没有误导.他们会在方法名称中准确地告诉您该方法何时/什么/为什么要使用.

Past tense. English may not be your first language, but a good understanding of English verb tense goes a really, really, really, really, really long way when working with Apple's method names. They're not misleading. They tell you exactly when/what/why that method is for, just in the method name.

那么,字符串应该适当地传递给视图控制器——你可能只是在错误的时间检查它.

Now then, the string SHOULD be appropriately passed into the view controller--you're probably just checking it at the wrong time.

但是对于标签栏,它包含的视图控制器不会在每次切换到该标签时加载.标签被预加载一次,并且只有在标签栏控制器本身被关闭(或者视图控制器可能从标签栏控制器的视图控制器数组中删除)时才会被卸载.

But with a tab bar, the view controllers it contains aren't loaded each time that tab is switched to. The tabs are preloaded once and will only ever be unloaded if the tab bar controller itself is dismissed (or perhaps the view controller is removed from the tab bar controller's view controllers array).

您可以使用 tabBarController:shouldSelectViewController:

这个方法,就像你正在使用的方法一样,为你提供了对标签栏控制器和它试图切换的视图控制器的引用.然而,它返回一个 BOOL 值.如果您返回 NO 切换将不会发生.如果你愿意,你可以总是简单地返回 YES,但这里的要点是这个方法在 switch 开始之前被调用,因此绝对在 viewWillAppearviewDidAppear 被调用.

This method, like the one you're using, gives you a reference to the tab bar controller and the view controller it's trying to switch too. It returns a BOOL value however. If you return NO the switch won't happen. You can simply always return YES if you want, the main point here though is that this method is called before the switch starts and therefore definitely before viewWillAppear and viewDidAppear are called.

这篇关于ios 将数据从 TabbarController 传递到 Storyboard 中的 ViewController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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