繁体   English   中英

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

[英]iOS: tab bar view controller using storyboard

我正在尝试完成以下任务:

我想做的是:

1.)当用户单击UIViewController中的按钮时,它将检索项目列表。
2.)这些项目将显示在UITableView中。
3.)最后,项目数将显示在第三个视图上(不是由UITableView发起的)

以上是我所管理的,只是我希望将所有这些控制器包装在自定义的标签栏控制器中。 但是数据没有从UIViewController传递到UITableView或其他View。 我正在使用Storyboard,我将标签栏控制器和其他3个控制器连接在一起作为视图控制器。 我尝试使用UIStoryboard方法实例化UITableView和UIView控制器并创建这些控制器的实例并将数据发送给它们,这些控制器显示在选项卡栏控制器中,但数据未显示。 顺便说一句,我不使用Segue。 我在这里想念什么吗?

谢谢。

码:

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]);

它们都是空的。

基本上,我有3个使用Storyboard连接到UITabbarController的“视图控制器”作为“视图控制器”。 我称其为上面的主视图控制器(在UITabbarController的索引为0处)仅具有一个UIButton。 一旦按下,它将获取一个字符串列表,并将其发送到Table View(在UITabbarController中的索引1),并在最后一个View Controller(在UITabbarController中的索引2)上,它将仅显示结果的字符串数。从主视图控制器而不是从表视图接收。

我在设置中没有使用任何UINavigationController。

我希望能够通过单击UITabbarController上的栏项目选项卡,甚至在显示表视图和结果视图控制器之前将其关闭。 触发是指将数据发送到“表视图”和“结果视图”,即使它们尚未显示。

  1. 您提供的代码对于您想要实现的代码是不必要的,也是错误的。 (通常,您应该有一个UIStoryboard实例。)阅读docs
  2. 您没有撰写任何有关如何尝试实现视图控制器之间的通信的文章。 有几种方法。 但是使用委派。 阅读文档
  3. 如果您回答评论,则应使用@字符,后跟SO用户名,以便通知他有关您的更新。

暂无
暂无

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

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