I'm using iOS 5 with Storyboard. My UITabBar
is created using the Interface Builder. I have two similar items in my TabBar
that is the same list, just with different type of item in it. What I've done, but looks weird to me, is setting a different "Tag" to each UITableView
and in the viewDidLoad
, and then assign the right type according to the Tag.
- (void)viewDidLoad
{
[super viewDidLoad];
if (self.tableView.tag == 1)
{
type = @"lent";
}
else if (self.tableView.tag == 2)
{
type = @"borrowed";
}
}
Any better way to do it? I'm not creating my UITabBar
in code, so my AppDelegate
is pretty empty! The type I set is just an attribute in one of my Core Data Entity, on a list I have Borrowed Items and on the other I have Lent Items, but they're the same entity.
You could expose the type as a property on your common view controller, and set it when the relevant tab bar item is selected (tabBarController:didSelectViewController:
from the UITabBarControllerDelegate protocol - your app delegate would be the tab bar controller delegate).
You would set this up as follows. Declare that your app delegate conforms to the UITabBarControllerDelegate protocol, then set it as the tab bar controller's delegate (you have to do this in code since the app delegate is not available to connect to in the storyboard). In your applicationDidFinishLaunching, add the following before you return YES:
UITabBarController *tbc = (UITabBarController*)self.window.rootViewController;
tbc.delegate = self;
Then implement the following delegate method:
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
switch (tabBarController.tabBar.selectedItem.tag)
{
case 1:
viewController.property = @"propertyA";
break;
case 2:
viewController.property = @"propertyB";
break;
}
NSLog(@"view controller is %@",viewController);
}
You'll need to cast the viewController variable to your actual view controller class, and also assign the relevant tags to the tab bar item of each view controller.
What you have there should work fine. Another option would be to have a common UIViewController super class that has all the functionality and then subclass that base class and provide an implementation of viewDidLoad that sets the appropriate type. Then in Interface Builder you can set the UITabBar view controllers as your appropriate subtypes.
The result would be the same, but it may be a bit more clear in IB what is going on because you don't have to rely on remembering the meaning of each numerical tag.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With