Linking to a view multiple times from tabar

ぃ、小莉子 提交于 2019-12-11 22:04:00

问题


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.

My setup is as follows TabBarController NavbarController - TabBarItem1 PeopleView NavbarController - TabBarItem2 ContentView

I want my setup to

TabBarController
    NavbarController1 - TabBarItem1
        Links to: PeopleView
    NavbarController2 - TabBarItem2
        Links to: ContentView
    NavbarController3 - TabBarItem3
        Links to: ContentView //Same VC as TabBaritem 2.

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'

Here is the code I have implemented:

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


回答1:


Try with this approach instead,

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

I uploaded the project here https://github.com/coch3/iOS---STCK-Q-19056515



来源:https://stackoverflow.com/questions/19056515/linking-to-a-view-multiple-times-from-tabar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!