Nicolò Ciraci
Nicolò Ciraci

Reputation: 678

UITabBarController in UINavigationController

I'm trying to put an UITabBarController inside an UINavigationController (Programmatically), this is my code:

UITabBarController *tabBarController = [[UITabBarController alloc] init];
HomeViewPhone *home = [[HomeViewPhone alloc] initWithStyle:UITableViewStylePlain];
home.title = NSLocalizedString(@"HOME",nil);
EventiPhone *eventi = [[EventiPhone alloc] initWithStyle:UITableViewStylePlain];
eventi.title = NSLocalizedString(@"EXPLORE", nil);
FavoritiPhone *favoriti = [[FavoritiPhone alloc] initWithStyle:UITableViewStylePlain];
favoriti.title = NSLocalizedString(@"FAVORITES",nil);
ProfiloPhone *profilo = [[ProfiloPhone alloc] initWithStyle:UITableViewStylePlain];
profilo.title = NSLocalizedString(@"PROFILE", nil);
[tabBarController setViewControllers:[NSArray arrayWithObjects:home,eventi,favoriti,profilo, nil]];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:tabBarController];
[self.window addSubview:navController.view];

but when I tap on one TabBarItem the app crash with this error

-[__NSCFString _tabBarItemClicked:]: unrecognized selector sent to instance 0x7934db0

Any ideas?

Upvotes: 0

Views: 723

Answers (2)

grominet
grominet

Reputation: 272

With ARC : solved with @property too !

More complicated app I have : AppDelegate -> NavigationController -> TableViewController -> TabBarController

First in AppDelegate, build the TableViewCtrl and insert in the NavCtrl

TableViewController *myTVC = [[TableViewController alloc] initWithStyle:UITableViewStylePlain];
UINavigationController *myNC = [[UINavigationController alloc] initWithRootViewController:myTVC];
[self.window setRootViewController:myNC];

Second, in TableViewCtrl method didSelectRow (for my use), pushViewController to the TabBarCtrl

_myTBC = [[TabBarController alloc] init];
[[self navigationController] pushViewController:_myTBC animated:YES];

Last, property the TabBarController in .h : that's the key !

@property (retain, nonatomic) UITabBarController * TabBar;

and build the .m, do your own...

ViewController1 *VC1 = [[ViewController1 alloc] init];
ViewController2 *VC2 = [[ViewController2 alloc] init];
_TabBar = [[UITabBarController alloc] init];
NSArray *table = [NSArray arrayWithObjects:VC1,VC2,nil];
[_TabBar setViewControllers:table animated:YES];
[[self view] addSubview:[_TabBar view]];

That's work nice! Doesn't need modal or other stuff...

Upvotes: 0

bearMountain
bearMountain

Reputation: 3980

Use

self.window.rootViewController = tabBarController;

Instead of

[self.window addSubview:navController.view];

Then add navBarControllers to any of the tabs that need them.

Upvotes: 4

Related Questions