0

我正在尝试设置我的应用程序,因此我可以将多个选项卡项链接到同一个视图控制器,但传入不同的参数以适当地设置视图。

我的设置如下 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,但我的应用程序总是因此错误而崩溃

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


}

最重要的是,我想用正确的方法来完成这一点,以避免使用三个类和屏幕来实现完全相同的功能。

对不起,如果这是一个非常基本的问题......

4

1 回答 1

0

尝试使用这种方法,

- (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

于 2013-09-27T17:32:55.417 回答