Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
M T
11,934 PointsPass data between tab bar view controllers programmatically
Unfortunately, didSelectViewController is only called when a user physically taps on a tab. If you switch tabs programmatically, the method is not called.
I'm trying to accomplish something similar to what would be done in a prepareForSegue method, where a property of the destination view controller is set prior to loading the view.
My attempt at passing values compiles, but I get a SIGABRT error "unrecognized selector sent to instance" for the value I set. After I hit a button, here's how I make my attempt in the function:
MyVC *destView = (MyVC *)[self.tabBarController.viewControllers objectAtIndex:2]; //get destination viewController
destView.item = self.item; //share item between viewControllers
[self.tabBarController setSelectedViewController:destView]; //change View
Or I could do [self.tabBarController setSelectedIndex:2]; for the last line.
Bummed this attempt didn't work. When I wrote something that didn't result in errors, I thought I was home free.
The property is set to nonatomic, strong.
Each solution seems just a step away from working. Thoughts?
M T
11,934 PointsUpdate: To get the view controller from the navigation controller, use topViewController like so:
UIViewController *destView = [[self.tabBarController.viewControllers objectAtIndex:2] topViewController];
However, if you check [destView class] isKindOfClass: [MyVC class] they will not equate even though if you log each of those independently you get "MyVC". No error will happen, it just won't pass the value.
Getting closer.
1 Answer
M T
11,934 PointsAnd answer: To check the class, use don't use [destView class], just use destView. And then everything works as hoped.
if ([destView isKindOfClass:[MyVC class]])
{
MyVC destinationViewController = (MyVC *)destView;
destinationViewController.item = self.item;
}
M T
11,934 PointsM T
11,934 PointsUpdate: it appears the class to tabBarController.viewControllers isn't a view controller, but a NavigationController. That's what is causing the "unrecognized selector set to instance" error.