从 Tabar 多次链接到视图 [英] Linking to a view multiple times from tabar
问题描述
我正在尝试设置我的应用程序,因此我可以将多个标签栏项目链接到同一个视图控制器,但传入不同的参数以适当地设置视图.
I am trying to setup my application, so I can have multiple tabbar items link to the same view controller,but pass in different parameters to setup the view appropriately.
我的设置如下标签栏控制器NavbarController - TabBarItem1人物视图NavbarController - TabBarItem2内容视图
My setup is as follows TabBarController NavbarController - TabBarItem1 PeopleView NavbarController - TabBarItem2 ContentView
我希望我的设置
TabBarController
NavbarController1 - TabBarItem1
Links to: PeopleView
NavbarController2 - TabBarItem2
Links to: ContentView
NavbarController3 - TabBarItem3
Links to: ContentView //Same VC as TabBaritem 2.
我尝试将下面的代码放在我的 appdelage 中并实现 UITabBarControllerDelegate,但我的应用程序总是因此错误而崩溃
I have tried putting the code below in my appdelage and implementing the UITabBarControllerDelegate, but my application always crashes with this error
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '- [UITabBarController pushViewController:animated:]: unrecognized selector sent to instance 0x8fce7c0'
这是我实现的代码:
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
NSLog(@"controller class: %@", NSStringFromClass([viewController class]));
NSLog(@"controller title: %@", viewController.title);
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
MediaList *mediaList = [storyboard instantiateViewControllerWithIdentifier:@"SB_MediaList"];
[(UINavigationController*)self.window.rootViewController pushViewController:mediaList animated:YES];
}
最重要的是,我希望以正确的方式来实现这一点,以避免为完全相同的功能提供三个类和屏幕.
More than anything I would like to proper way to accomplish this to avoid having three classes and screens for the exact same functionality.
对不起,如果这是一个非常基本的问题......
Sorry if this is a very basic question....
推荐答案
试试这个方法,
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
DemoViewController *demoView = [storyboard instantiateViewControllerWithIdentifier:@"DemoView"];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:demoView];
NSMutableArray *viewsArray = [NSMutableArray arrayWithArray:self.viewControllers];
if (viewController.tabBarItem.tag == 1) {
//Load data for your demoView instance for tabBarItem 1
} else if (viewController.tabBarItem.tag == 2) {
//Load data for your demoView instance for tabBarItem 2
}
[viewsArray replaceObjectAtIndex:viewController.tabBarItem.tag withObject:navigationController];
self.viewControllers = viewsArray;
}
我在这里上传了项目https://github.com/coch3/iOS---STCK-Q-19056515
这篇关于从 Tabar 多次链接到视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!