简体   繁体   中英

Pass data through view controller, tab bar controller, navigation controller and to view controller

This is probably something really simple to achieve, but I am new to programming for iOS and I seem to be stuck.

So, basically, I have a tabbed application. I decided that I wanted a navigation bar, in addition to the tab bar. To do this, I put the tab bar controller and then I added my view controllers and embedded in a navigation controller for each view controller, which is then connected to the tab bar.

My hierarchy in the storyboard looks somewhat like this:

  1. View Controller
    1. Tab Bar Controller
      1. Navigation Controller
        1. View Controller
      2. Navigation Controller
        1. View Controller

The part where I am stuck here, is when attempting to pass data from the first View Controller and to any of the other View Controllers. Before adding in the navigation controllers, I was using the prepareForSegue method to pass the data, like so:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([[segue identifier] isEqualToString:@"logged"])
    {
        UITabBarController *tabar=segue.destinationViewController;
        SecondViewController *svc=[tabar.viewControllers objectAtIndex:1];
        svc.groupArray = [(NSArray*)sender objectAtIndex:0];
        svc.userArray = [(NSArray*)sender objectAtIndex:1];
        svc.taskArray = [(NSMutableArray*)sender objectAtIndex:2];
        svc.selfArray = [(NSMutableArray*)sender objectAtIndex:3];
        [tabar setSelectedIndex:1];
    }
}

As you can see, I was passing the data to my second view controller and set the tab bar index to 1 using the performSegueWithIdentifier method, since I wanted the second page to open. All of this was working just fine, until I introduced the Navigation Controllers to my code, since I want navigation bars. That's when everything pretty much broke. If I try to run the code as is, the application crashes with the following output in the console:

[UINavigationController setGroupArray:]: unrecognized selector sent to instance 0x7ffa6acec620

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UINavigationController setGroupArray:]: unrecognized selector sent to instance 0x7ffa6acec620'

I've tried messing around with the code a bit, but nothing seems to work really. I am just very confused, and maybe a hint in the right direction would help me out a bit.

Since you embedded your view controllers inside navigation controllers they are no longer direct children of the tabbar; however, you are casting the children of the tabbar controller as something other than Navigation controllers. You want to first get the navigation controller, which is a child of the tabbar controller, and then get the child of that navigation controller. This will be your view controller. Then you can set the data for that.

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([[segue identifier] isEqualToString:@"logged"])
   {
       UITabBarController *tabar=segue.destinationViewController;
       UINavigationController *navController = [tabbar.viewControllers objectAtIndex:1];
       SecondViewController *svc=[navController.viewControllers objectAtIndex:0];
       svc.groupArray = [(NSArray*)sender objectAtIndex:0];
       svc.userArray = [(NSArray*)sender objectAtIndex:1];
       svc.taskArray = [(NSMutableArray*)sender objectAtIndex:2];
       svc.selfArray = [(NSMutableArray*)sender objectAtIndex:3];
       [tabar setSelectedIndex:1];
   }
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([[segue identifier] isEqualToString:@"logged"])
    {
        UITabBarController *tabar=segue.destinationViewController;
        UINavigationController *navc =[tabar.viewControllers objectAtIndex:1]; 
        SecondViewController *svc=[nvc.viewControllers objectAtIndex:0];
        svc.groupArray = [(NSArray*)sender objectAtIndex:0];
        svc.userArray = [(NSArray*)sender objectAtIndex:1];
        svc.taskArray = [(NSMutableArray*)sender objectAtIndex:2];
        svc.selfArray = [(NSMutableArray*)sender objectAtIndex:3];
        [tabar setSelectedIndex:1];
    }
}

As you have changed UITabbarController root with UINavigationController, hence while fetching viewcontrollers from UITabbarController it will be of navigation controller type.

Then you have to fetch SecondViewController from navigation controller.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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