Retaining the same tab bar in a new view - ios

I am a newbie with regards to IOS Development, so I really need help regarding this matter. I have been working in a program where in a have a slide bar as well as a tab bar. However, when I click an item from the slide bar, it goes to the new view and the tab bar disappears. I tried embedding the new view into a navigation controller and adding a push segue between my tab bar controller a to the new view.. but still, tab bar won't show up.
What will I do to retain or use my existing tab bar in my new view? so, it will be visible to the new view.
Thanks! your help is very much appreciated. :)

First, you gotta perform a push, not a modal. Then, you could use this method hidesbotTombarWhenPushed in the view controller that pushes, that should do the trick.
If not, maybe you can clarify more or post some code :)

This may be because of you are not adding tabview controller as the window's root view controller in AppDelegate. Check the sample code below
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
viewController = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
searchController = [[SearchController alloc]initWithNibName:#"SearchController" bundle:nil];
navCtrl1 = [[UINavigationController alloc]initWithRootViewController:viewController];
navCtrl2 = [[UINavigationController alloc]initWithRootViewController:searchController];
NSMutableArray *viewControllersArray = [[NSMutableArray alloc]init];
[viewControllersArray addObject:navCtrl1];
[viewControllersArray addObject:navCtrl2];
tabController = [[UITabBarController alloc]init];
tabController.viewControllers = [NSArray arrayWithObjects: navCtrl1 ,navCtrl2,nil];
self.window.rootViewController = tabController;
[self.window makeKeyAndVisible];
return YES;
}

Related

iOS navigation controller without navigation bar

This is how I launched by main controller in my iOS app.
UIViewController *rootController = [[RootViewController alloc] init];
navigationController = [[UINavigationController alloc] initWithRootViewController:rootController];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[self.window setRootViewController:navigationController];
[self.window makeKeyAndVisible];
return YES;
With this I am getting a blank bar at the top.
I want to use a navigation controller to control the flow of screens that users are able to reach by pressing back but I don't want a navigation bar. How can I change my code to get what I want?
You can use this code right after you declare navigationController = ... in the code sample you provided:
[navigationController setNavigationBarHidden:true];
And then whenever you need to go "back," your app would run this code from within the current view controller:
[self.navigationController popViewControllerAnimated:true];

navigation controller navigation bar only covers top left half of screen in plain vanilla code

I'm just doing some ios programming after not touching it for a few months, and I'm new to it this year. I just started a new project, and all I did was programmatically set up a navigation controller as the root view controller.
Here's the code:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
UINavigationController *nvc = [[UINavigationController alloc] init];
nvc.view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
nvc.view.backgroundColor = [UIColor yellowColor];
nvc.title = #"Woah";
// nvc.navigationBarHidden = TRUE; this works ok
self.window.rootViewController = nvc;
return YES;
}
I don't have the reputation points to post an image, but here it is: http://i.stack.imgur.com/tbcqi.png
As you can see the title does not show in the navigation bar. Also, quite oddly, the navigation bar is only across the top left corner and not across the entire view. What am I missing here? Also if I set the commented out line to run, to hide the bar, that does work. But of course I want the bar, I don't not want it.
This is a brand new project I just started fresh, and I tried it in two new projects. I have the same outcome in both cases of this weird left corner bar. Any ideas what I am doing wrong? Thanks.
You need to add a UIViewController as the root of you UINavigationController. At the moment you have no view controllers being managed.
The navigation bar title reflects the contents of each UIViewControllers UINavigationItem. By default the title on the navigation bar is the title of the UIViewController which is top most in the navigation stack.
The back button oddly reflects the title of the UINavigationItem of the UIViewController second from top. So to set the back button label you actually set it in the navigationItem of the controller which pushed the top controller on.
You can change the title by accessing the top view controllers navigationItem or from inside the top view controller using:
<a view controller>.navigationItem.title = #"My New Title";
or if from within the controller:
self.navigationItem.title = #"My New Title";
I am not sure what is your intention to create UIWindow because it's the code for ages ago.
Now when you are upgrade your xCode to the latest version, there's no need to UIWindow to be created manually.
Also, when you are creating UINavigationController, it is much better when you are setting UINavigationController's rootView controller.
I am quite sure there will be UIViewController which need to be nested in UINavigationController.
So your code will need to be changed like below;
HomeViewController *homeVC = [[HomeViewController alloc] initWithNibName:#"HomeViewController"]; //Let's assume this is the first VC on the navigation stack
UINavigationController *nvc = [[UINavigationController alloc] initWithRootViewController:homeVC];
// nvc.view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// nvc.view.backgroundColor = [UIColor yellowColor]; use nvc.navigationBar.tintColor = [UIColor yellowColor];
// nvc.title = #"Woah"; use homeVC.title = #"Woah";
// nvc.navigationBarHidden = TRUE; this works ok
self.window.rootViewController = nvc;

Navigation Controller with TabBarController in App

I am trying to make a tableview with two views, the tableview controller and the otherView controller. The tableview needs a navigation controller to get to a third view when a cell is clicked. I tried using the code below in my AppDelegate.m but it just creates the tableview with the navigation controller. Any suggestions on how I should edit this to get the tabview working as well? Thanks!
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
AJBTableViewController *masterViewController = [[AJBTableViewController alloc] initWithNibName:nil bundle:nil];
self.navigationController = [[UINavigationController alloc] initWithRootViewController:masterViewController];
self.window.rootViewController = self.navigationController;
[self.window makeKeyAndVisible];
_mainTabBar.viewControllers = [NSArray arrayWithObjects:_navigationController, otherViewController, nil];
[_window addSubview:_mainTabBar.view];
return YES;
}
The tab bar controller should be the root view controller of the app, not the navigation controller of the first tab bar item.
_mainTabBar.viewControllers = [NSArray arrayWithObjects:_navigationController, otherViewController, nil];
[_window addSubview:_mainTabBar.view];
Instead this code, you try below code:
UITabBarController *tbC = [[UITabBarController alloc]init];
tbC.viewControllers = [NSArray arrayWithObjects:_navigationController,otherViewController, nil];
self.window.rootViewController = tbC;
and delete
self.window.rootViewController = self.navigationController;
Sorry for the stupid error, I never allocated an instance of the tabbar controller, Mundi is right, making the rootviewcontroller the tabbarcontroller is the way to go.

iOS initial log in view before navigational Controller

I have been playing around with this for a couple of days and I cannot figure this out.
-> Basically I want to implement a simple login view that has a button when clicked, goes to go to the navigation controller ( in my case is "viewController" with buttons that link to mini math games which are other views).
-> Login screen should be displayed first, than navigation controller's root view when a button is clicked on the login screen
-> I have tried to declare the navigation controller when I click the button of the login screen but that seems to not work
-> Is it safe to say that a navigation controller can only be initialized in the apple delegate?
Currently I have this in my apple delegate declaring and setting my navigational controller:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions: (NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.viewController = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
UINavigationController *navigationViewController = [[UINavigationController alloc] initWithRootViewController:self.viewController]; // self.viewController is the main screen
self.window.rootViewController = navigationViewController; // set root to navigationViewController
[self.window makeKeyAndVisible];
return YES;
}
Any ideas will be appreciated. Thank you for your time !
Your code in the app delegate looks ok. NavigationController does not need to be declared in the AppDelegate. In your case, it is definitely ok to declare it upon login button pressed.
Try this at the login event:
UIViewController *nextVC = [[UIViewController alloc] init];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:nextVC];
[self presentViewController:navController animated:YES completion:^{
}];
What I would do would be set the loginViewController as the rootViewController of the navigation. And after check if login was done successfully, you could implement [self performSegueWithIdentifier:#"identifier"] setting the game viewController as destination. (Using Storyboard would make your life much easier). Well, that's my opinion :)

UINavigationController is NULL

I've had a good look at the Apple docs as well as similar Stack Overflow questions, but I am stuck on why my navigationController is null when using tab bars. I am trying to build most of the app from code, and am not using XIBs to insert a navigationController.
While debugging I have greatly simplified my app, down to two tabs. One tab holds a tableview and when a row is touched I'm expecting a detail page (from a XIB) to appear. Should be pretty simple. I am finding the value of self.navigationController is NULL when attempting to push the detail view, and of course it is then not working. I took the tab bar our completely and it works fine from a single view (the tableview). In this instance self.navigationController has a value.
AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// With Tab Bars
self.tabBarController = [[UITabBarController alloc] init];
ViewController *vc1 = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
vc1.tabBarItem.title = #"Words";
vc1.tabBarItem.image = [UIImage imageNamed:#"tab_feed.png"];
TextTableViewController *vc2 = [[TextTableViewController alloc] init];
vc2.tabBarItem.title = #"Text";
vc2.tabBarItem.image = [UIImage imageNamed:#"tab_live.png"];
UINavigationController* navController = [[UINavigationController alloc] initWithRootViewController:vc1];
NSArray* controllers = [NSArray arrayWithObjects:vc2, navController, nil];
tabBarController.viewControllers = controllers;
tabBarController.delegate = self;
window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
window.rootViewController = self.tabBarController;
[window makeKeyAndVisible];
return YES;
}
From TextTableViewController.m
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
TextViewController *detailViewController = [[TextViewController alloc] initWithNibName:#"TextViewController" bundle:nil];
Text *text = [[Text alloc] init];
text = [textArray objectAtIndex:indexPath.row];
detailViewController.TextID = text.textID;
NSLog(#"Nav Controller: %#",self.navigationController);
[self.navigationController pushViewController:detailViewController animated:YES];
NSLog(#"pushed");
}
I've also got two questions related to this problem.
(1) What is the purpose of this line. It doesn't appear to make a difference if it is in or out, and is absent from the Apple example.
tabBarController.delegate = self;
(2) When creating an array of tabs, one of the views is made the navigationController. Does it matter which tab it is or should this be a different view altogether not related to any tab and not visible. Is this where the problem lies?
In answer to question (1) about the tab bar controller's delegate, see the UITabBarControllerDelegate protocol reference. For the basic functionality of a tab bar controller, you don't need to bother with a delegate.
But let's say you want to do something special--for instance, save a document or reset an interface element to a default value--when the user changes tabs. You could make one of your classes, perhaps your app delegate or another controller class, conform to the UITabBarControllerDelegate protocol and implement tabBarController:didSelectViewController:
In your "answer" you asked if each tab will need its own UINavigation controller. That is absolutely correct. Basically, each tab is a completely independent hierarchy, so you need a separate UINavigation controller in each tab that requires one.
This should also imply the answer to your question (2) in the original post. You need to add the nav controller to the specific tab(s) that needs it.
OK I found it. The UINavigationController needs to be contained within the appropriate tab of the UITabBarController. So by making this coding change (below), a new UINavigationController is embedded in the tab with the tableview.
UINavigationController* navController = [[UINavigationController alloc] initWithRootViewController:vc2];
NSArray* controllers = [NSArray arrayWithObjects:vc1, navController, nil];
Which then begs the question: what if you need multiple examples of this - do you create a new UINavigationController for each tab that has a need for one, and mark each one as a rootViewController?

Resources