Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Kizmar

macrumors newbie
Original poster
Oct 28, 2010
22
0
Again, .NET developer that's new to iOS/Mac-type development.

I'm attempting to use a TabBar delegate method "didSelectViewController" to intercept a tab change and set a property on the view controller when a specific tab is set. Everything is "working" except the property's value appears to have lost persistence by the time I arrive at the view controller's "viewDidLoad" method.

I need to back up a little bit and figure out what the "best practice" is when doing something like this. Should I be using a global variable instead of trying to set a property of the view controller?

Or... am I doing something wrong in the declaration of the property? Here's what I did on the view controller header:
Code:
...
@interface EntityListViewController: UIViewController {
  @public
      NSString *listType;
      NSNumber *someID;
}

@property (retain) NSString *listType;
@property (nonatomic, retain) NSNumber *someID;
...

Then the .m file:
Code:
...
  @synthesize listType;
  @synthesize someID;
...

The delegate:
Code:
// Optional UITabBarControllerDelegate method.
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
   if (viewController.tabBarItem.tag == 2) {
       // Set a property of the currently selected view
       ((EntityListViewController *)viewController).listType = @"Testing";
    }
}

With this, by the time I grab the value of "listType" in the "viewDidLoad" method of EntityListViewController, it's empty.

Just need a point in the right direction. Thanks. :)
 
I suspect things are happening in a different order than you think they are: viewDidLoad is most likely being called before tabBarController:didSelectViewController:. This makes sense if you think about it: tabBarController:shouldSelectViewController: is called before the tab is selected. The tab is then selected (which will load the view if required firing viewDidLoad). Then once the selection is completed tabBarController:didSelectViewController: is called.
 
I didn't think about that. Can I get to "viewController.tabBarItem.tag" from the "viewDidLoad" method?
 
Maybe. I'm honestly not sure. I think so, but I'm not sure if viewDidLoad is called before or after the view is inserted into the view hierarchy. The best I can suggest is to give it a shot and see.
 
Thanks for the troubleshooting ideas. I was stumped there. :)
 
It appears that both "viewWillAppear" and "viewDidLoad" are hit before the tab bar delegate method "didSelectViewController".

I'm at a loss here.

All I'm trying to do is set a global variable between the "touch up inside" event of a tab bar item and the "viewWillAppear" or "viewDidLoad" methods. That or I need to interrogate the tab bar from the active view and see that the current tab's tag is. Can't figure that out either.

Arg! :(
 
OK, but it's not really clear why? Can you not just set it when you set which view controller is assigned to each tab?
 
OK, but it's not really clear why? Can you not just set it when you set which view controller is assigned to each tab?

This is where my lack of experience is killing me I think. I haven't quite figured out if/how I can use the Interface Builder to get this tab to fire off an action when it's selected (because it's the MainWindow.xib). That nib and the tab bar controller are still a bit of a black box to me.
 
Does viewWillAppear: not do that? I.e. only get called when the view will appear as the tab is selected?

So are you saying "viewWillAppear" will only fire off if the view is being made active as a result of a tab bar item? I haven't gotten far enough to test this if that is the case. Theoretically this view will end up being called from several different places within in the app (including the tab bar).
 
Why not use tabBarController:shouldSelectViewController:, which is a method already pointed out, and one that should be called BEFORE a view controller is selected.
 
viewWillAppear: is only called when the view will be presented on screen. So it should be called when that view becomes selected in a tab controller, when it becomes visible in a navigation stack (either you push it or the stack gets popped back to it) etc.
 
Why not use tabBarController:shouldSelectViewController:, which is a method already pointed out, and one that should be called BEFORE a view controller is selected.

Apparently I don't know *how* to use this method. As soon as I add it I end up with a "SIGABRT". AppDelegate.m...
Code:
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {
	return YES;
}
 
I'm abandoning this. It's obviously way over my head. Thanks for trying to help.
 
Use the debugger. Start by setting a breakpoint.

Yeah I tried all that. I don't know where to look for useful exception information when the app aborts and bails. Trying different methods. We'll see.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.