Just created a new project and I have 4 view controllers which I add to UINavigationController like this:
WatchViewController *first = [[WatchViewController alloc] init];
BetViewController *second = [[BetViewController alloc] init];
Settings *third = [[Settings alloc] init];
Account *forth = [[Account alloc] init];
UINavigationController *navFirst = [[UINavigationController alloc]initWithRootViewController:first];
UINavigationController *navSecond = [[UINavigationController alloc]initWithRootViewController:second];
UINavigationController *navThird = [[UINavigationController alloc]initWithRootViewController:third];
UINavigationController *navForth = [[UINavigationController alloc]initWithRootViewController:forth];
Load them into an array:
NSArray *viewArray = [[NSArray alloc] initWithObjects:navFirst, navSecond, navThird, navForth, nil];
Load the tab bar and window:
self.tabController = [[UITabBarController alloc] init];
[self.tabController setViewControllers:viewArray animated:YES];
[self.window setRootViewController:self.tabController];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
All the view are just standard views. When I try and run the app it responds with :
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Pushing a navigation controller is not supported'
I can't figure out what I've missed. Any help?
Don't create 4 navigation controllers. Controllers that are needed to navigate through should be assigned to viewControllers property in UINavigationController via setViewControllers:animated: method.
You should create 1 NavigationController and add array of 4 UIViewControllers.
A very good example is given here: example and don't forget to look here UINavigationClass
Try this one:
WatchViewController *first = [[WatchViewController alloc] initWithNibName:#"WatchViewController" bundle:Nil];
BetViewController *second = [[BetViewController alloc] initWithNibName:#"BetViewController" bundle:Nil];
Settings *third = [[Settings alloc] initWithNibName:#"Settings" bundle:Nil];
Account *forth = [[Account alloc] initWithNibName:#"Account" bundle:Nil];
/*Your View Navigation Stuff and your viewArray*/
self.tabController.viewControllers = viewArray;
Why don't you try UINavigationController inside UITabBarController by setting controllers in xib. It worked for me.
Sorry, I had annoyingly subclassed UINavigationController instead of UIViewController. I hadn't picked up on it because if I didn't use the NavControllers the App would run fine, add the nav controllers and it broke. :(
Related
After searching a lot for a solution to my problem and didn't found a correct answer I post the problem: I have two viewControllers company and companyDetail when I execute
CompanyViewDetail *cDetail = [[CompanyViewDetail alloc] init];
[self presentViewController:cDetail animated:YES completion:nil];
it navigate to companyDetailViewController but the tabBar that I created in AppDelegate disappear, here is the code:
tabBars = [[UITabBarController alloc] init];
self.window.rootViewController = tabBars;
self.tabBars.view.autoresizingMask = (UIViewAutoresizingFlexibleHeight);
[self.window makeKeyAndVisible];
I am changing UITabBarItem title after the user changes his language preferences in the app settings. The problem is that the whole item disappears for a while after this change is made and then appears again with the new title.
In AppDelegate I am initializing UITabBarController:
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.delegate = self;
BFNCategoriesTableViewController *shopViewController = [[BFNCategoriesTableViewController alloc] init];
UINavigationController *shopNavigationController = [[UINavigationController alloc] initWithRootViewController:shopViewController];
shopNavigationController.tabBarItem = [[UITabBarItem alloc] initWithTitle:BFNLocalizedString(kTranslationShop) image:[UIImage imageNamed:#"TabBarShopUnselected"] selectedImage:[UIImage imageNamed:#"TabBarShopSelected"]];
self.tabBarController.viewControllers = #[offersNavigationController,
shopNavigationController,
//wishlistNavigationController,
cartNavigationController,
moreNavigationController];
Then after the translation is downloaded I just set different title like this:
navigationController.title = BFNLocalizedString(kTranslationCart);
Do you have an idea why this happens and eventually how to ovecome this issue?
Thanks.
Firstly, I can't understand regarding code above, how tabBarController is connected with UINavigationController.
You should
self.tabBarController.viewControllers = [NSArray arrayWithObject: shopViewController];
UINavigationController *shopNavigationController = [[UINavigationController alloc] initWithRootViewController: self.tabBarController ];
UINavigationController doesn't have a property is tabBarItem.
Following that:
shopViewController.title = = BFNLocalizedString(kTranslationCart);
It changes a title is on top of navController and title of tabBar as well.
Another way, It can change a tabBarTitle directly.
UITabBar *tabBar = self.tabBarController.tabBar;
UITabBarItem * itemBar = [ [UITabBarItem alloc] initWithTitle:BFNLocalizedString(kTranslationCart)
image:(UIImage *)image // your image
selectedImage:(UIImage *)selectedImage]; // highlighted image
[tabBar setItems:[NSArray arrayWithObject:itemBar
animated:YES ];
I think a first way is enough it must certainly work.
I started using UITabBarController and it is great.
The thing is I have a few views that aren't accessed from the UITabBar
(either presented modal programatically or we want to have a button on the top to jump to them)
The thing is that I want to retain the Tab bar visible in these views.
From my understanding mixing presentViewController and UITabBarController is problematic.
How can I do that? Can I have "hidden" tab bar elements I can reference programatically?
Just to clarify with an example:
Views A,B,C,D are in the tab bar - via the storyboard - everything is peachy.
I NEED to have views E and F clickable from the top navigation (please don't suggest a sliding TabBar or a multiple line UITabBar).
I could just jump to E and F but I want the UITabBar to still be visible so the user can jump from E to A for example.
Just use the good old UINavigationController for every tab and just use [self.navigationController pushViewController:A animated:YES];
That's how the setup looks in code:
SGTabBarViewController *rootVC = [[SGTabBarViewController alloc] init];
SGFirstTabViewController *firstVC = [[SGFirstTabViewController alloc] init];
UINavigationController *navController1 = [[UINavigationController alloc] initWithRootViewController:firstVC];
SGSecondTabViewController *secondVC = [[SGSecondTabViewController alloc] init];
UINavigationController *navController2 = [[UINavigationController alloc] initWithRootViewController:secondVC];
SGThirdTabViewController *thirdVC = [[SGThirdTabViewController alloc] init];
UINavigationController *navController3 = [[UINavigationController alloc] initWithRootViewController:thirdVC];
SGForuthTabViewController *fourhtVC = [[SGForuthTabViewController alloc] init];
UINavigationController *navController4 = [[UINavigationController alloc] initWithRootViewController:fourhtVC];
rootVC.viewControllers = #[navController1, navController2, navController3, navController4];
self.window.rootViewController = rootVC;
[self.window makeKeyAndVisible];
If you want your UITabBar visible on every VC you push just use hidesBottomBarWhenPushed = NO; on it.
However, there is no way to have UITabBar visible on views presented modally.
I have 5 views in my app and I'm appDelegate by setting them in the following way:
UITabBarController *tabBarController = [[UITabBarController alloc] init];
tabBarController.viewControllers = [NSArray arrayWithObjects:navigationcontroller1, navigationcontroller7, navigationcontroller5, navigationcontroller4, navigationcontroller6, navigationcontroller2, nil];
self.window.rootViewController = tabBarController;
All of them come with a NavigationController and tabbarcontroller, But I needed to split the screen into two parts, in this case the screen would be divided would be navigationcontroller2, as you can see below:
VendaViewController *venda_viewcontroller = [[VendaViewController alloc] init];
UINavigationController *navigationcontroller2 = [[UINavigationController alloc] init];
[navigationcontroller2 pushViewController:venda_viewcontroller animated:YES];
Hence I tried the following way:
VendaViewController *venda_viewcontroller = [[VendaViewController alloc] init];
VendaDetailViewController *vendaDetail_viewcontroller = [[VendaDetailViewController alloc] init];
UISplitViewController *splitVC = [[UISplitViewController alloc] init];
[splitVC setViewControllers:[NSArray arrayWithObjects:venda_viewcontroller,vendaDetail_viewcontroller,nil]];
UINavigationController *navigationcontroller2 = [[UINavigationController alloc] init];
[navigationcontroller2 pushViewController:splitVC animated:YES];
But not work in this code, but in documentation of UISplitViewController is writing the following message:
"you must always install the view from a UISplitViewController object
as the root view of your application’s window. [...] Split view
controllers cannot be presented modally."
So...If I like to put a splitViewController in my view controller, I'll have to put splitViewController in all of my views controllers? Or have another solution to this?
You can use a UISplitViewController only as the root view controller of your app. In your case you can implement your custom container view controller with functionality similar to the split (two subview controller inside the main). Follow this link for details.
I have experience in C and C++, but near zero experience in Objective C, or Xcode4.
Im looking to create an app with a Tab bar, Navigation Bar, and Table Views. Based on the knowledge i have i assume i start from the top and drill down to the root?
First
Create myTableViewController class that will dynamically create tableview content and push its created view onto the navigation controller.
Then...
Create myNavController class that holds myTableViewController. with a method that creates a new item for myTableViewController.
Then...
Create the Tab Bar Controller that has the above as one of its tabs in an array along with some other tabs, set the tab bar controller as the root controller and display it to the window.
Is this the right direction to be thinking? Or am i horribly off course?
I have an app with these same requirements. It's got a UITabBar, and in the different tabs each UITableViewController has a UINavigationController navigation bar at the top.
Here's how my App Delegate handles this:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
//Create the UITabBarController
UITabBarController *tabBarController = [[UITabBarController alloc] init];
//Create the view controllers for our tabs
UITableViewController *vc1 = [[UITableViewController alloc] init];
UITableViewController *vc2 = [[UITableViewController alloc] init];
UITableViewController *vc3 = [[UITableViewController alloc] init];
UITableViewController *vc4 = [[UITableViewController alloc] init];
UITableViewController *vc5 = [[UITableViewController alloc] init];
//Create the Navigation Controllers for these views
UINavigationController *nc1 = [[[UINavigationController alloc]
initWithRootViewController:vc1] autorelease];
UINavigationController *nc2 = [[[UINavigationController alloc]
initWithRootViewController:vc2] autorelease];
UINavigationController *nc3 = [[[UINavigationController alloc]
initWithRootViewController:vc3] autorelease];
UINavigationController *nc4 = [[[UINavigationController alloc]
initWithRootViewController:vc4] autorelease];
UINavigationController *nc5 = [[[UINavigationController alloc]
initWithRootViewController:vc5] autorelease];
//Make an array containing the view controllers
NSArray *viewControllers = [NSArray arrayWithObjects:nc1, nc2, nc3, nc4, nc5, nil];
//The NSArray has retained these controllers, we can now release them.
[vc1 release];
[vc2 release];
[vc3 release];
[vc4 release];
[vc5 release];
[nc1 release];
[nc2 release];
[nc3 release];
[nc4 release];
[nc5 release];
//Assign the view controllers to the tab bar.
[tabBarController setViewControllers:viewControllers];
//Set tabBarController as rootViewController of window
[self.window setRootViewController:tabBarController];
//The window retains tabBarController, we can release our reference
[tabBarController release];
[self.window makeKeyAndVisible];
return YES;
}
Enjoy!