0

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];
}

enter image description here

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
}
}
programcode
  • 104
  • 1
  • 14

1 Answers1

2

How to push a view controller using a UITabBar using Objective C?

That question doesn't make much sense if you think about it. "Push" is a stack operation, and a navigation controller manages a stack of view controllers. UITabBar isn't a view controller at all, and it doesn't manage a stack of anything, so pushing isn't an operation that's available.

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.

There's a reason for that, and the reason is that navigation controllers and tab controllers offer different modes of controlling what the user sees on the screen. If a tab controller could be part of a navigation stack, the user would see the tab bar appear and disappear, when it's intended to be something that's fixed. It would create a very confusing interface.

If you use a tab bar alone to get around the fact that Cocoa Touch discourages you from creating a confusing user interface, you're going to end up with a confusing user interface. Think hard about what you're really trying to do, and if you decide you still need to do it, consider using UI elements that don't look like a tab bar so that the user doesn't expect tab bar behavior.

Caleb
  • 124,013
  • 19
  • 183
  • 272
  • I edited the title of the question to be less confusing. It looks like you're right. How would you achieve the functionality in question? Isn't a UITabBar still an option? – programcode Jan 02 '22 at 20:16
  • The project has a sign up and login process so I don't want to show a tabBarController on every View Controller. Just the View Controller that is shown after logging in. I'm trying to show a new View Controller that the user will need to see in order to understand how to use the app and I figured a UITabBar would help achieve this. – programcode Jan 02 '22 at 20:25
  • I get what you mean. What about the Instagram app for instance? It doesn't show a tabBarController on the first view but after logging in it does? Do you think the UITabBar in the above code can still be used for this type of functionality? I really just want to show a new view when the `Rules` tab is tapped. – programcode Jan 02 '22 at 20:37
  • 1
    @programcode You know you can change the root view controller, right? So show your login interface if you need to, but then switch the root view controller when the user logs in. Switch back if they log out. – Caleb Jan 02 '22 at 21:43
  • Can you please elaborate? Are you saying to use a tab bar controller? I think I tried that and the tab bar controller changed the navigation bar icons when the new view was loaded. I can try again. – programcode Jan 03 '22 at 00:52
  • Can you please share some code for this? – programcode Jan 03 '22 at 01:02
  • 1
    I’m saying that if you want a tab-based interface, you can use a tab bar controller… just make it the root view controller. – Caleb Jan 03 '22 at 01:05
  • 1
    There are lots of existing questions with code… try this one: https://stackoverflow.com/q/15774003 – Caleb Jan 03 '22 at 01:12
  • Got it. So Ill just need to hide the tab bar controller on the View Controllers that aren't supposed to show it? Also, would I need code for your suggestion or can the storyboard handle this? The only code I want to use is hiding the tab bar controller on the views that don't need it. – programcode Jan 03 '22 at 02:04
  • So I tried using a Tab Bar Controller as a root view and added the View Controller relationships to my desired views. Things went well until I tapped the first tab bar item twice and it took me all the way back to the sign up process even after signing in. Any ideas? – programcode Jan 03 '22 at 02:33