Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITabBar - two views (nib) same Class using Storyboard

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.

like image 490
allaire Avatar asked Oct 09 '22 23:10

allaire


2 Answers

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.

like image 156
jrturton Avatar answered Oct 11 '22 13:10

jrturton


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.

like image 23
nickbona Avatar answered Oct 11 '22 11:10

nickbona