Setting a view controller in a tab bar controller - ios

My app has tab bar controller that loads 3 view controllers in its viewDidLoad method.
- (void)viewDidLoad
{
[super viewDidLoad];
...
[self setViewControllers:#[firstViewController,
secondViewController,
thirdViewController,
]];
}
I want it to show up with a view controller (homeViewController) that is different from these three controllers. When the tab bar is first loaded none of these three tab bars will be selected. I want to change them by pressing tab bar items and return to home view by pressing navigation left bar button.
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:btnImage
style:UIBarButtonItemStylePlain
target:self
action:#selector(setHomeView)];
How can I show the homeViewController when the tab bar controller is first loaded without added it to tab bar items?

The tab bar controller by default sets the first ViewController at index0 to be the home controller.
If you don't want the first ViewController of the application to live inside of the tabBarController then you have to create your HomeVC and set it as the entry point for the application, but the tab bar won't be present in that View.
Theres no way to present a view controller inside of the TabBarController unless it lives somewhere inside of those three navigation stacks.

Related

Why doesn't the tab bar show up on my view controllers?

I have the following in Interface Builder:
The top left is my main view controller where I have 2 buttons that have segue to two UIViewControllers. These two UIViewControllers are linked with the Tab Bar Controller. However, how could I make those 2 buttons to link to specifically to one/other views? Right now it's connected specifically, but it (or something else) causes the bar tab not show up.
Is it the problem that I don't have the Tab Bar Controller connected to the main view?
Yes, you're right that the problem occurs because the tab bar controller needs to be the destination of the segues. Fix it like this:
In IB, erase the segues from the two buttons and create two new ones, one from each button to the tab bar controller. Give each one an identifier, like buttonA from one button and buttonB from the other.
In the view controller, implement prepareForSegue for each segue understanding that the destination is a tab bar controller and that each segue requires a different tab selection...
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"buttonA"]) {
UITabBarController *tabBarController = (UITabBarController *)segue.destinationViewController;
tabBarController.selectedIndex = 0;
}
if ([segue.identifier isEqualToString:#"buttonB"]) {
UITabBarController *tabBarController = (UITabBarController *)segue.destinationViewController;
tabBarController.selectedIndex = 1;
}
}
That's not quite how the tabBarController works.
I can see your initial view controller is the one on the top left, and it pushes either of the other two on the right on to the navigation stack if you push a button. But in your current setup, at no point does the tab controller itself get pushed on to the stack.
Instead, you would want to have your initial view controller push the tab bar controller on to the stack, through a button or otherwise, and the tab bar controller will display your other two view controllers as its setup to do.

Multiple segues remove navigation bar in view controller

I have a view controller that has a navigation bar. It is the root view controller of the navigation controller. I have another view controller that can be segued from a button on the first controller. Up to this point, everything works fine; there are navigation bars on both view controllers.
However, from the second view controller, I want to be able to segue back to the first controller. When doing this, it removes the navigation bar from both of the view controllers.
How can I get the navigation bar on both view controllers with buttons as transitions? Thanks!
Implement below method in your both viewcontroller,
- (void) viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self.navigationController setNavigationBarHidden:NO animated:NO];
}
You may hiding navigationbar somewhere.
Try this in storyboard:
Select the navigation controller -> Attributes Inspector -> Under Navigation controller and Bar Visibility leave the "Show navigation bar" blank

Navigation Bar & Navigation Toolbar NOT from App Delegate

I'm new to xcode ios 7.
I've struggling a lot with navigation controls building app for iPhone ios7.
I don't want to use storyboard. I prefer to do it programmatically.
What i am trying to do.
I know how to create NavigationBar & Navigation Toolbar via AppDelegate.m
But if it's possible i don't want to do it, because let's assume i want in FirstViewController to show just simple button "Go to Second View Controller" (No Navigation Bar or Toolbar here).
And now in SecondViewController i want to create Navigation Toolbar (bottom) with 4 different tabs linking to ViewControllers!
But the trick is, i want to maintain different Navigation Bar (top) for every ViewController (as you can see in this screenshot).
Here is my Xcode Project File
And here is screenshot form Fancy app showing what i am trying to achieve.
Thanks a lot in advance!
Just to give you an idea, When you tap the button on your first view controller, you can create a UINavigationController and set your second view controller as its root view controller. This way, your first view controller remains no nav bar view and the second view controller holds a Navigation controller. Try something like below:
-(IBAction)goToSecondVC:(id)sender // An action for the button in your first VC
{
SecondViewController *secondVC = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil]; //Init with second view controller
UINavigationController *secondNavVC = [[UINavigationController alloc] initWithRootViewController:secondVC]; // create a navigation controller and set the root as your second view controller
[self presentViewController:secondNavVC animated:YES completion:nil]; // and then present it with anim or no anim..
}

how to go to tab bar controller after clicking the respective button

I have structure of project as shown below.
On the left side I have MainViewController. There I have two buttons as English and Arabic. What I want to do is when I click English, I want to go English tab bar controller (HomeViewController).
Hence what I wrote is
- (IBAction)langEnglish:(id)sender {
HomeViewController *secondView = [self.storyboard instantiateViewControllerWithIdentifier:#"enghome"];
[self.navigationController pushViewController:secondView animated:YES];
}
This is working perfectly, but I don't see tab bar.
Tab bar is missing from this.
Any idea what is going wrong?
Basically what I have is view controller as main controller and upon clicking button on this controller, tab view controller should get open...
Go to your main storyboard and select your main view controller. On top select Editor->Embed in->navigation bar.
EDIT: If this does not work push to your tab bar and use this code:
self.tabBarController.selectedIndex = 1;
What I did is as below
- (IBAction)langEnglish:(id)sender {
EngTabViewController *secondView = [self.storyboard instantiateViewControllerWithIdentifier:#"engtab"];
[self.navigationController pushViewController:secondView animated:YES];
}
EngTabViewController is the UITabBarController and I assigned id to tab bar controller... this works like charm...
Means instead of viewcontorller, I used tabbarcontroller...
You shouldn't push a UITabBarController on a UINavigationController. Set it as the window's root view controller instead and the tab bar should be displayed as expected.

Move from view controller to tab bar controller

When you create a tab bar controller via the interface and not programatically you dont have a tab bar controller class. How do you move from a view controller to this tab bar controller in a case such as this?
You could try and create the tab bar controller first. Then show the full-screen splash screen modally (e.g. without animation, so that user won't notice the tab bar controller) and off that splash screen you present location selector. Once the user is done with it you dismiss both modal controllers revealing tab controller.
If you need to set up view controllers in your tab bar controller based on the information you're getting from location selector you can do it e.g. via a delegate, that is:
Implement delegate for splash screen and location selector
Instantiate tab bar controller
Show the splash screen, setting current vc of tab bar controller a delegate of the splash screen
Show location selector, setting its delegate to the same view controller (you'll have the handle in splash screen)
Once user dismisses location selector you inform the delegate about it and it's able to collect needed data from the location selector.
I hope it helps.
At first make the tabbarcontroller then make uiview controller ...such as...
NSMutableArray *listOfViewControllers = [[NSMutableArray alloc] init];
tab1view = [[tab1viewcontroller alloc] initWithNibName:#"tab1viewcontroller" bundle:nil];
[listOfViewControllers tab1view];
[tab1view release];
tab2view = [[tab2viewcontroller alloc] initWithNibName:#"tab2viewcontroller" bundle:nil];
[listOfViewControllers tab2view];
[tab2view release];
[self.tabBarController setViewControllers:listOfViewControllers animated:YES];
i think it will help you.

Resources