My goal for this project is to cycle between View Controllers using a UITabBar, without a UITabBarController, because according to Apple docs, TabBarControllers should not be pushed to UINavigationControllers, which this project uses already.
So far, I am using this UITabBar
from @samuel's answer from this question. How to add UITabBar in iphone using objective c
//viewDidLoad
...
UITabBar *tabBar = [[UITabBar alloc] initWithFrame:CGRectMake(0, 431, 320, 50)];
[self.view addSubview:tabBar];
[[UITabBar appearance] setBarTintColor:[UIColor blackColor]];
NSMutableArray *tabBarItems = [[NSMutableArray alloc] init];
UITabBarItem *tabBarItem = [[UITabBarItem alloc] initWithTitle:@"Home" image:[UIImage imageNamed:@""] tag:0];
UITabBarItem *tabBarItem1 = [[UITabBarItem alloc] initWithTitle:@"Rules" image:[UIImage imageNamed:@""] tag:1];
[tabBarItems addObject:tabBarItem];
[tabBarItems addObject:tabBarItem1];
tabBar.items = tabBarItems;
tabBar.selectedItem = [tabBarItems objectAtIndex:0];
}
I am ready to cycle between View Controllers when I press the tabBarItems. What is the correct way to do this? I want to go to a View Controller named ViewController9
when the Rules
tabBarItem
is tapped, and then to ViewController6
when the Home
tabBarItem
is tapped.
Can someone please share some code? Here's what I've tried but nothing happens when the tabBarItems are pressed:
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {
NSInteger selectedTag = tabBar.selectedItem.tag;
NSLog(@"%ld",(long)selectedTag);
if (selectedTag == 0) {
//Do what ever you want here
NSString * storyboardName = @"MainStoryboard";
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyboardName bundle: nil];
UIViewController * vc = [storyboard instantiateViewControllerWithIdentifier:@"ViewController9"];
[self presentViewController:vc animated:YES completion:nil];
} else if(selectedTag == 1) {
NSString * storyboardName = @"MainStoryboard";
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyboardName bundle: nil];
UIViewController * vc = [storyboard instantiateViewControllerWithIdentifier:@"ViewController9"];
[self presentViewController:vc animated:YES completion:nil];
} else { //if(selectedTag == 2)
//Do what ever you want here
}
}