Idan Moshe
Idan Moshe

Reputation: 1535

PresentModalViewController not showing TabBar

I'm trying to go from one xib to another and I'm using TabBar. When I move from xib to xib with PresentModalViewController I lose the TabBar.

When I use this way, it fail (like force close in android):

FirstViewController *fvc = [[FirstViewController alloc]initWithNibName:@"FirstViewController" bundle:nil];
    [fvc setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];
    fvc.userSelectedLatitude = saveLatitude;
    fvc.userSelectedLongitude = saveLongtitude;
    UITabBarController *tabControl = [[UITabBarController alloc] initWithNibName:fvc bundle:nil];
    [self presentModalViewController:tabControl animated:NO];

When I use:

UITabBarController *tabControl = [[UITabBarController alloc] initWithNibName:@"FirstViewController" bundle:nil];

I get black screen with TabBar.

Since it all fail I guess it is not the right way. So, what is the right way to do it?

Upvotes: 1

Views: 909

Answers (2)

thxou
thxou

Reputation: 691

The code above crashes because you are trying to pass a view controller instead of an NSString object in the initWithNibName:bundle: method.

The way to do it depends of what you really want to do. Do you want to present the xib in a modalViewController with or without a tabBar?, or simply present the view controller modally?.

UPDATE:

Fine, you have to create first your view controllers associated to each tabBar button (like you have been doing until now), after, add these view controllers to your tabBar and then present the tabBarController modally. Like this:

FirstViewController *fvc = [[FirstViewController alloc] initWithNibName:@"FirstViewController" bundle:nil];
fvc.userSelectedLatitude = saveLatitude;
fvc.userSelectedLongitude = saveLongtitude;

UITabBarController *tabControl = [[UITabBarController alloc] init];
[tabControl setViewControllers:[NSArray arrayWithObjects:fvc, nil]];
[tabControl setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];
[self presentModalViewController:tabControl animated:NO];

I think this code should work. So, try it and tell us if something goes wrong.

Upvotes: 2

Ed Chin
Ed Chin

Reputation: 1214

With UITabBarController, there is no need to manually present viewControllers or call code to switch the views. This is handled for you.

All you need to do is set the viewControllers property of the UITabBarController. Like so:

[tabBarController setViewControllers:[NSArray arrayWithObjects:view1, view2, nil]];

Upvotes: 1

Related Questions