将数据传递给Tabbar控制器 [英] Passing Data to Tabbar Controller

查看:108
本文介绍了将数据传递给Tabbar控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个故事板项目,我想将一些数据从视图传递到标签栏控制器,信息将在标签之间展开。在做了一些研究后,我发现了一个非常类似的问题: iOS故事板传递数据navigationViewController 但是这个解决方案唯一的问题是它没有转移到标签栏控制器。我想知道我是否必须将数据传递给每个标签,还是可以将其传递给标签栏控制器然后从那里传播?感谢您的帮助!

I have a storyboard project and I would like to pass some data from a view into a tab bar controller, the information will be spread out between the tabs. After doing some research I found a very similar issue: iOS storyboard passing data navigationViewController but the only issue with this solution is that it was not transferring to a tab bar controller. I was wondering would I have to pass the data to each tab or can I pass it to the tab bar controller and then spread it from there? Thank you for your help in advance!

我目前正在使用以下内容;但是,我收到一个错误:

I am currently using the following; but, I get an error:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
     if ([segue.identifier isEqualToString:@"FoodPage"]) {
    NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
    UINavigationController *nav = [segue destinationViewController];
    FoodViewController *destViewController = (FoodViewController*) nav.topViewController;
    destViewController.Foods = [foodArray objectAtIndex:indexPath.row];
  }
}

推荐答案

为了获得对<$的引用c $ c> UINavigationController 在您的情况下,您必须执行以下操作并为 tabBarController 首先指定正确的索引

In order to get a reference to your UINavigationController in your case, you have to do the following and specify the correct index to your tabBarController first:

UINavigationController *nav = [self.tabBarController.viewControllers objectAtIndex:<THE_INDEX_NUMBER_FOR_YOUR_NAVIGATION_CONTROLLER];

完成后,您将检索对 FoodViewController <的引用/ code>再次在 UINavigationController 上指定它的索引号(即如果它在顶部,那么 0 ):

Once you have done so, then you retrieve a reference to your FoodViewController by specifying again the index number for it on your UINavigationController (i.e. if it's on top, then 0):

FoodViewController *destViewController = (FoodViewController*) [self.nav.viewControllers objectAtIndex:0];






更新


Update:

现在我已经更好地了解了您想要实现的目标:

Now that I got a better idea what you would like to achieve:

您正在使用 UITabbarController ,其中嵌入了其他控制器。

You are using UITabbarController in which your others controllers are embedded.

场景/示例案例

Scenario / Example Case:

假设我们分别有2个视图控制器,控制器A和控制器B,两者都嵌入在 UITabbarController

Let say we had 2 view controllers, controller A and controller B, respectively, both are embedded in a UITabbarController.

我们想要什么:

What we want:

我们正在尝试从控制器A更改控制器B中 UILabel 的文本。

We are trying to change the text of a UILabel in controller B from controller A.

首先,在.h中的控制器B中声明一个属性

First, declare a property in controller B in .h:

@property(strong, nonatomic) UILabel *aLabelInControllerB;

第二个,在控制器中声明一个属性(或ivar)A:

Second, declare a a property (or ivar) in your controller A:

@property(strong, nonatomic) ControllerB *controllerB;

因为你使用 UITabbarController 你不要需要使用segue,你可以简单地
获得 UITabbarController 通过 self.tabBarController ;

Since you are using UITabbarController you don't need to use segue, you could simply get a hold of UITabbarController via self.tabBarController;

问题 :当我的标签栏控制器被点击然后更改文本时,我怎么知道控制器B中的标签?

Question: "how would I know then when my tab bar controller is tapped and then change the text of the label in controller B?"

我们这样做:

设置控制器A作为 UITabbarController的代表 by:

Set controller A as the delegate of UITabbarController by:

在控制器A .h中,添加:

In controller A .h, add:

@interface <Your_Controller> : UIViewController <UITabBarControllerDelegate>

在控制器A的 viewDidLoad 中:

self.tabBarController.delegate = self;

在控制器A .m中,实现此方法:

And in controller A .m, implement this method:

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
    self.controllerB = (ControllerB *) [tabBarController.viewControllers objectAtIndex:1];
    //In our example here, we only have 2 view controllers (A and B)
    //So, index 1 is where controller B resides.

    self.controllerB.aLabelInControllerB.text = @"Hello!";
    //This will change the text of the label in controller B
}

当控制器B出现时,您将看到标签的文本将更改为Hello!

And as controller B appears, you will see that the text of the label will be changed to "Hello!"

设置 NSString 遵循相同的程序。

Ex: self.controllerB.stringInControllerB = @来自控制器B! ;

希望有所帮助。

更新2:

Update 2:

Segue'ing from表格单元格到标签栏控制器? Oki ..这是解决方案。我在我的例子中只使用了一个单元格,所以如果你希望将来有更多的单元格,我会把它留给你调整。

Segue'ing from table view cell to a tab bar controller? Oki.. Here is the solution. I am only using one cell in my example, so should it become desired that you would like to have more cells in the future, I would leave that up to you to adjust.

让我们来看看故事板布局:

Let's take a look at the storyboard layout:

在故事板中, control 从您的单元格拖动到标签栏控制器。

In storyboard, control drag from your cell to the tab bar controller.

在图片中添加 UINavigationController

UITableViewController中 .m:

添加2个属性:

@property (strong, nonatomic) UITabBarController *myTabbarController;
@property (strong, nonatomic) YourFirstViewController *myFirstViewController;

只是一个友情提醒:

Just a friendly reminder:

请记住添加:

self.tableView.delegate = self;
self.tableView.dataSource = self;

在表格视图控制器中添加以下内容:

Add the following in your table view controller:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    self.myTabbarController = (UITabBarController*) [segue destinationViewController];
    self.myFirstViewController = [self.myTabbarController.viewControllers objectAtIndex:0];
    self.myFirstViewController.stringFromTableViewController = @"Hi from TableViewController!";
}

并且在 myFirstViewController ,在.h中添加2个属性:

And in myFirstViewController, add 2 properties in .h:

@property (strong, nonatomic) NSString *stringFromTableViewController;
@property (strong, nonatomic) YourSecondViewController *secondViewController;

与上面第二次编辑一样,让你的第一个视图控制器仍然是UITabBarControllerDelegate的委托并实现此方法:

Like from the second edit above, have your first view controller to be still the delegate of UITabBarControllerDelegate and implement this method:

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
    self.secondViewController = (YourSecondViewController*) viewController;
    self.secondViewController.aLabel.text = self.stringFromTableViewController; 
}

就像以前一样,中无需更改任何内容SecondViewController

我自己跑了;应该很适合你的设置。

I ran this myself; should be good to go for your setup.

享受。

这篇关于将数据传递给Tabbar控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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