4

I'm trying to pass some data between my views in a tab bar. my first view is able to load the data from my model class and manipulate it. But when I hit the second or third tab in my tab bar controller, the data doesn't get passed. Here's how I'm attempting to pass it.

-(void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{

if (tabBarController.selectedIndex == 1){
HashTagTableViewController *hash [[HashTagTableViewController alloc]init];
    hash.userArray = feed.userArray;
}else if (tabBarController.selectedIndex == 2){
    PhotoTagTableViewController *photo = [[PhotoTagTableViewController alloc]init;
    photo.userArray = feed.userArray;

}

}

feed is the name of the instance of my model class I created in the current view controller. I'm trying to avoid making multiple instances of the model class since it has to make multiple calls to an API. All I'm trying to do is pass the feed.userArray to the different views to be manipulated differently.

Droppy
  • 9,691
  • 1
  • 20
  • 27
S.G.
  • 71
  • 3
  • So `feed` is your model data. You are doing the right thing and using the MVC pattern correctly. Now you need to set some breakpoints and examine what the data looks like during the set-up phase and then from the view controller's perspective. – Droppy Jun 25 '15 at 07:17
  • During this phase, the hash.userArray is set just fine. But when it switches to the HashTagTableViewController, hash.userArray turns out empty. – S.G. Jun 25 '15 at 16:42

2 Answers2

3

Do not create view controllers in this method. UITabBarController automatically creates all view controllers on initialisation. Try this instead:

-(void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{
    if (tabBarController.selectedIndex == 1){
        HashTagTableViewController *hash = (HashTagTableViewController *) viewController;
        hash.userArray = feed.userArray;
    }else if (tabBarController.selectedIndex == 2){
        PhotoTagTableViewController *photo = (PhotoTagTableViewController *)viewController;
        photo.userArray = feed.userArray;
    }
}
Ram Suthar
  • 356
  • 2
  • 9
  • Crashed and got this error: [UINavigationController setUserArray:]: unrecognized selector sent to instance 0x7fc5fb4cf880 – S.G. Jun 25 '15 at 16:36
0

You are creating new ViewController instance. Instead of this you need to get the selected viewcontroller from the TabBarController's ViewController array. I have changed your code. So check it below.

NOTE:

Please solve the Spelling mistake/Method names. Because i have write this in the notepad. Not in xcode.

-(void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
    if([viewController isKindOfClass: [HashTagTableViewController class]]) 
    {   
        HashTagTableViewController *hash = (HashTagTableViewController) viewController;
        hash.userArray = feed.userArray;
    }
    else if([viewController isKindOfClass: [PhotoTagTableViewController class]]) 
    {
        PhotoTagTableViewController *photo = (PhotoTagTableViewController) viewController;
        photo.userArray = feed.userArray;
    }       
}
V.J.
  • 9,492
  • 4
  • 33
  • 49
  • Yes. It is passed in viewController. You are right @Droppy – V.J. Jun 25 '15 at 07:27
  • That was my first attempt. Using the debugger, it skips over the first if statement since the viewController class is UINaviagtionController class. – S.G. Jun 25 '15 at 16:35