将数据从视图控制器传递到iOS中的标签栏控制器
我正在开发一款iOS应用,现在我很茫然。我正在尝试将数据从第一个View Controller传递到TabBarViewController的第一个选项卡(使用storyboard)。我找到了许多教程来解释如何在视图控制器之间传递数据,但是我的标签栏没有任何效果。我知道标签栏控制器包含一种视图数组。视图控制器和标签栏控制器之间的关系使用segue(推送)实现。所以,我认为使用prepareForSegue()方法很容易。像那样:
I'm developing an iOS app and now I'm at a loss. I'm trying to pass data from the first View Controller to the first tab of a TabBarViewController (with using the storyboard). I found a lot of tutorials that explain how to pass data between view controllers, but nothing worked with my tab bar. I know that the tab bar controller contains a kind of array of views. The relation between the View Controller and the Tab Bar Controller is realized using a segue (push). So, I thought it is easy to use the prepareForSegue() - method. Like that:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if ([[segue identifier] isEqualToString:@"tab"]) {
// ...
}
}
不幸的是,标签栏控制器和每个标签栏视图之间的关系不是真正的segue。这只是一种关系。这意味着,我没有能够用于上述方法的segue标识符。在这种情况下是否有可能使用prepareForSegue?如果没有,任何想法如何解决这个问题?我知道有一个类似的问题,但答案并没有那么有用。我是否必须为标签栏控制器中的每个选项卡(视图)创建新文件?或者是否可以为整个标签栏控制器设置一个类(m。& h。),使用objectAtIndex()访问多个视图?
Unfortunately, the relation between the Tab Bar Controller and each tab bar view is no real segue. It's only a "relationship". That means, there is no segue identifier I am able to use for the the method above-mentioned. Is there a possibility to use the prepareForSegue in this case? If not, any ideas how to solve this problem? I know that there is a similar question, but the answer wasn't that helpful. Do I have to create a new file for every tab (view) within the tab bar controller? Or is it possible to have one class (m. & h.) for the whole tab bar controller, accessing the several view with objectAtIndex()?
提前感谢!
这是我的设置有效:
- 设置Segue:
- Setup Segue:
- 设置视图控制器,带有segue到标签栏控制器,故事板中有2个子视图控制器
- 指定segue标识符(
标签
)
- Setup View Controller with segue to Tab Bar Controller with 2 child View Controllers in Storyboard
- Specify segue identifier (
tab
)
- View Controller class =
ViewController
- Tab Bar Controller class =
TabBarController
- 标签栏控制器子视图控制器类=
TabsViewController
(两者共享)
- View Controller class =
ViewController
- Tab Bar Controller class =
TabBarController
- Tab Bar Controller Child View Controller class =
TabsViewController
(shared between both)
设置 labelString
标签栏控制器中的属性:
Setup labelString
property in Tab Bar Controller:
-
在
TabBarController.h
:
@property (nonatomic, strong) NSString *labelString;
在 TabBarController.m
中:
@synthesize labelString;
设置 prepareForSegue
中的方法ViewController.m
:
#import "TabBarController.h"
...
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if ([[segue identifier] isEqualToString:@"tab"]){
TabBarController *tabBar = [segue destinationViewController];
[tabBar setLabelString:[NSString stringWithFormat:@"This has been set"]];
}
}
设置 UILabel
用于子选项卡栏视图控制器。
Setup UILabel
s for Child Tab Bar View Controllers.
- 将默认
UILabel
控件拖到两个子视图控制器中 -
在
中创建属性TabsViewController.h
:
- Drag default
UILabel
controls into both child View Controllers Create property in
TabsViewController.h
:
@property (nonatomic, strong) IBOutlet UILabel *label;
挂钩 5.1
和 5.2
在故事板中
设置 ViewDidLoad
中的方法TabsViewController.m
:
#import "TabBarController.h"
...
@synthesize label;
...
- (void)viewDidLoad
{
[super viewDidLoad];
TabBarController *tabBar = (TabBarController *)self.tabBarController;
label.text = [NSString stringWithFormat:@"Tab %i: %@",[tabBar.viewControllers indexOfObject:self],tabBar.labelString];
}
现在点击第一个和第二个标签将显示标签标签0:已设置
和标签1:已设置
分别。
Now clicking on the 1st and 2nd tabs will have the labels display Tab 0: This has been set
and Tab 1: This has been set
, respectively.