简体   繁体   English

iOS:使用情节提要的标签栏视图控制器

[英]iOS: tab bar view controller using storyboard

I am trying to accomplish the following: 我正在尝试完成以下任务:

What I would like to do is: 我想做的是:

1.) When the user clicks on a button in UIViewController, it will retrieve a list of items. 1.)当用户单击UIViewController中的按钮时,它将检索项目列表。
2.) The items will then be displayed in UITableView. 2.)这些项目将显示在UITableView中。
3.) Lastly the number of items will be displayed on the third view (not initiated by UITableView) 3.)最后,项目数将显示在第三个视图上(不是由UITableView发起的)

All the above I manage, except that I would like to wrap all these controllers in a customized tab bar controller. 以上是我所管理的,只是我希望将所有这些控制器包装在自定义的标签栏控制器中。 But the data is not being passed from the UIViewController to the UITableView or the other View. 但是数据没有从UIViewController传递到UITableView或其他View。 I am using Storyboard, I hooked up tab bar controller and the other 3 controllers there as view controllers. 我正在使用Storyboard,我将标签栏控制器和其他3个控制器连接在一起作为视图控制器。 I have tried instantiating UITableView and the UIView controllers using UIStoryboard method and creating instances of these controllers and send the data to them, the controllers are shown in tab bar controller, but data is not. 我尝试使用UIStoryboard方法实例化UITableView和UIView控制器并创建这些控制器的实例并将数据发送给它们,这些控制器显示在选项卡栏控制器中,但数据未显示。 I am not using Segue, by the way. 顺便说一句,我不使用Segue。 Am I missing something here? 我在这里想念什么吗?

Thanks. 谢谢。

CODE: 码:

UITabBarController tabBarController = (UITabBarController ) self.window.rootViewController;

  UIStoryboard *storyboard1 = [UIStoryboard storyboardWithName:@"MainStoryboard_iPhone" bundle:[NSBundle mainBundle]]; mainViewController *mainController = (mainViewController*)[storyboard1 instantiateViewControllerWithIdentifier:@"mainController"]; UIStoryboard *storyboard2 = [UIStoryboard storyboardWithName:@"MainStoryboard_iPhone" bundle:[NSBundle mainBundle]]; tableViewController *tableview = (tableViewController*)[storyboard2 instantiateViewControllerWithIdentifier:@"tableviewController"]; UIStoryboard *storyboard4 = [UIStoryboard storyboardWithName:@"MainStoryboard_iPhone" bundle:[NSBundle mainBundle]]; resultController *mapView = (resultController*)[storyboard4 instantiateViewControllerWithIdentifier:@"resultView"];</code> I have tried also creating the UI Table View and the result view controller in MainViewController and pass data to them (before they are even displayed), but no data is ever present when these controllers are displayed. I made sure I alloc and init these controllers properly and NSLog shows that the instance methods in these controllers are called, but still no data is shown, even though the controllers are displayed in tab bar view controller, when they are displayed. UPDATE 2: Now I am trying to do the following: self.tableviewController = [self.tabBarController.viewControllers objectAtIndex:1]; 
self.resultViewController = [self.tabBarController.viewControllers objectAtIndex:2];

UIStoryboard *storyboard1 = [UIStoryboard storyboardWithName:@"MainStoryboard_iPhone" bundle:[NSBundle mainBundle]]; mainViewController *mainController = (mainViewController*)[storyboard1 instantiateViewControllerWithIdentifier:@"mainController"]; UIStoryboard *storyboard2 = [UIStoryboard storyboardWithName:@"MainStoryboard_iPhone" bundle:[NSBundle mainBundle]]; tableViewController *tableview = (tableViewController*)[storyboard2 instantiateViewControllerWithIdentifier:@"tableviewController"]; UIStoryboard *storyboard4 = [UIStoryboard storyboardWithName:@"MainStoryboard_iPhone" bundle:[NSBundle mainBundle]]; resultController *mapView = (resultController*)[storyboard4 instantiateViewControllerWithIdentifier:@"resultView"];</code> I have tried also creating the UI Table View and the result view controller in MainViewController and pass data to them (before they are even displayed), but no data is ever present when these controllers are displayed. I made sure I alloc and init these controllers properly and NSLog shows that the instance methods in these controllers are called, but still no data is shown, even though the controllers are displayed in tab bar view controller, when they are displayed. UPDATE 2: Now I am trying to do the following: self.tableviewController = [self.tabBarController.viewControllers objectAtIndex:1];
self.resultViewController = [self.tabBarController.viewControllers objectAtIndex:2];

UIStoryboard *storyboard1 = [UIStoryboard storyboardWithName:@"MainStoryboard_iPhone" bundle:[NSBundle mainBundle]]; mainViewController *mainController = (mainViewController*)[storyboard1 instantiateViewControllerWithIdentifier:@"mainController"]; UIStoryboard *storyboard2 = [UIStoryboard storyboardWithName:@"MainStoryboard_iPhone" bundle:[NSBundle mainBundle]]; tableViewController *tableview = (tableViewController*)[storyboard2 instantiateViewControllerWithIdentifier:@"tableviewController"]; UIStoryboard *storyboard4 = [UIStoryboard storyboardWithName:@"MainStoryboard_iPhone" bundle:[NSBundle mainBundle]]; resultController *mapView = (resultController*)[storyboard4 instantiateViewControllerWithIdentifier:@"resultView"];</code> I have tried also creating the UI Table View and the result view controller in MainViewController and pass data to them (before they are even displayed), but no data is ever present when these controllers are displayed. I made sure I alloc and init these controllers properly and NSLog shows that the instance methods in these controllers are called, but still no data is shown, even though the controllers are displayed in tab bar view controller, when they are displayed. UPDATE 2: Now I am trying to do the following: self.tableviewController = [self.tabBarController.viewControllers objectAtIndex:1];
self.resultViewController = [self.tabBarController.viewControllers objectAtIndex:2];

UIStoryboard *storyboard1 = [UIStoryboard storyboardWithName:@"MainStoryboard_iPhone" bundle:[NSBundle mainBundle]]; mainViewController *mainController = (mainViewController*)[storyboard1 instantiateViewControllerWithIdentifier:@"mainController"]; UIStoryboard *storyboard2 = [UIStoryboard storyboardWithName:@"MainStoryboard_iPhone" bundle:[NSBundle mainBundle]]; tableViewController *tableview = (tableViewController*)[storyboard2 instantiateViewControllerWithIdentifier:@"tableviewController"]; UIStoryboard *storyboard4 = [UIStoryboard storyboardWithName:@"MainStoryboard_iPhone" bundle:[NSBundle mainBundle]]; resultController *mapView = (resultController*)[storyboard4 instantiateViewControllerWithIdentifier:@"resultView"];</code> I have tried also creating the UI Table View and the result view controller in MainViewController and pass data to them (before they are even displayed), but no data is ever present when these controllers are displayed. I made sure I alloc and init these controllers properly and NSLog shows that the instance methods in these controllers are called, but still no data is shown, even though the controllers are displayed in tab bar view controller, when they are displayed. UPDATE 2: Now I am trying to do the following: self.tableviewController = [self.tabBarController.viewControllers objectAtIndex:1];
self.resultViewController = [self.tabBarController.viewControllers objectAtIndex:2];

UIStoryboard *storyboard1 = [UIStoryboard storyboardWithName:@"MainStoryboard_iPhone" bundle:[NSBundle mainBundle]]; mainViewController *mainController = (mainViewController*)[storyboard1 instantiateViewControllerWithIdentifier:@"mainController"]; UIStoryboard *storyboard2 = [UIStoryboard storyboardWithName:@"MainStoryboard_iPhone" bundle:[NSBundle mainBundle]]; tableViewController *tableview = (tableViewController*)[storyboard2 instantiateViewControllerWithIdentifier:@"tableviewController"]; UIStoryboard *storyboard4 = [UIStoryboard storyboardWithName:@"MainStoryboard_iPhone" bundle:[NSBundle mainBundle]]; resultController *mapView = (resultController*)[storyboard4 instantiateViewControllerWithIdentifier:@"resultView"];</code> I have tried also creating the UI Table View and the result view controller in MainViewController and pass data to them (before they are even displayed), but no data is ever present when these controllers are displayed. I made sure I alloc and init these controllers properly and NSLog shows that the instance methods in these controllers are called, but still no data is shown, even though the controllers are displayed in tab bar view controller, when they are displayed. UPDATE 2: Now I am trying to do the following: self.tableviewController = [self.tabBarController.viewControllers objectAtIndex:1];
self.resultViewController = [self.tabBarController.viewControllers objectAtIndex:2];

And when I call these:
NSLog(@"%@", [self.tabBarController class]);
NSLog(@"%@", [self.tableViewController class]);
NSLog(@"%@", [self.resultViewController class]);

They are all null. 它们都是空的。

Basically, I have 3 View Controllers hooked up as "view controllers" using storyboard to a UITabbarController. 基本上,我有3个使用Storyboard连接到UITabbarController的“视图控制器”作为“视图控制器”。 The main view controller (at index 0 in UITabbarController), from which I call the above, simply has one UIButton. 我称其为上面的主视图控制器(在UITabbarController的索引为0处)仅具有一个UIButton。 Once pressed it will fetch a list of strings and send it to the Table View (at index 1 in UITabbarController) and on the last View Controller (at index 2 in UITabbarController) it will show just the number of strings as a result, which it receives from the Main View Controller, not from Table View. 一旦按下,它将获取一个字符串列表,并将其发送到Table View(在UITabbarController中的索引1),并在最后一个View Controller(在UITabbarController中的索引2)上,它将仅显示结果的字符串数。从主视图控制器而不是从表视图接收。

I am not using any UINavigationController in my set up. 我在设置中没有使用任何UINavigationController。

I would like to be able to fire off Table View and the Result View Controller before they are even displayed by clicking on the bar item tab on UITabbarController. 我希望能够通过单击UITabbarController上的栏项目选项卡,甚至在显示表视图和结果视图控制器之前将其关闭。 By firing off I mean the data will be sent over to Table View and Result View even though they are not yet displayed. 触发是指将数据发送到“表视图”和“结果视图”,即使它们尚未显示。

  1. The code you present is unnecessary and wrong for what you want to achieve. 您提供的代码对于您想要实现的代码是不必要的,也是错误的。 (Usually you should have one instance of UIStoryboard.) Read the docs . (通常,您应该有一个UIStoryboard实例。)阅读docs
  2. You do not write anything about how you try to achieve the communication between the view controllers. 您没有撰写任何有关如何尝试实现视图控制器之间的通信的文章。 There are several methods. 有几种方法。 But use delegation. 但是使用委派。 Read the docs . 阅读文档
  3. If you answer to a comment you should use the @ character followed by the SO user name so he is notified about your updates. 如果您回答评论,则应使用@字符,后跟SO用户名,以便通知他有关您的更新。

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

相关问题 查看标签栏 controller 从 storyboard swift 5 - view tab bar controller from storyboard swift 5 将核心数据与情节提要中的选项卡栏控制器一起使用 - using Core Data with tab bar controller in storyboard 在Storyboard xCode中从View Controller切换到Tab Bar Controller - Switch from View Controller to Tab Bar Controller in a Storyboard xCode 故事板-从模态视图控制器返回到选项卡栏控制器 - Storyboard - go back to tab bar controller from modal view controller 在iOS中,如何使用相同的标签栏动态加载视图控制器 - In iOS, how to dynamically load a view controller using the same tab bar 如何在故事板的标签栏上启动导航控制器的第三个视图控制器 - how to start third view controller of navigation controllers on tab bar in storyboard 如何在Tab Bar Controller Storyboard中添加视图 - How to add view inside Tab Bar Controller Storyboard 如何在不同故事板的标签栏上显示拆分视图控制器? - How to show split view controller on tab bar from different storyboard? 故事板使用segue将数据从视图控制器传递到标签栏 - Storyboard pass data from view controller to tab bar with segue 在iOS中显示新视图和标签栏控制器 - Displaying a new view and Tab Bar Controller in iOS
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM