Adding extra Tab Buttons to Tab Bar - objective-c

I started a new Tabbed Application in XCode and I was wondering what the best way to add extra buttons to the given Tab Bar is?
Thanks!

As According to Xcode 4.3.3 When we Create Tabbed Application then it gives two ViewController. So if You want to add more you can go in AppDelegate and you can see
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
UIViewController *viewController1 = [[[FirstViewController alloc] initWithNibName:#"FirstViewController" bundle:nil] autorelease];
UIViewController *viewController2 = [[[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil] autorelease];
self.tabBarController = [[[UITabBarController alloc] init] autorelease];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:viewController1, viewController2, nil];
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
return YES;
}
And you can see Two ViewController are added in the tabBarController and You can create new one and add init as 1 or 2 were added.
Try .......

Related

UiTabBarController's tabs aren't showing at all but functional, what could be causing this issue?

As you can see in the image below the tab bar is visible but the tabs aren't. However I am still able to click where both of the tabs would be and flick between 2 views.
What could be wrong?
AppDelegate Code:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
HypnosisViewController *hvc = [[HypnosisViewController alloc] init];
TimeViewController *tvc = [[TimeViewController alloc] init];
UITabBarController *tabBarController = [[UITabBarController alloc] init];
NSArray *viewControllers = [NSArray arrayWithObjects:hvc, tvc, nil];
[tabBarController setViewControllers:viewControllers];
[[self window] setRootViewController:tabBarController];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
Regards
You haven't declared any titles or images for your TabBar items.
Use something like this: initWithTabBarSystemItem:tag:

UITabBarController doesn't carry through when i push a new view from a UINavigationController

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions: (NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
FirstViewController *fvc = [[FirstViewController alloc] init];
SecondtViewController *svc = [[FirstViewController alloc] init];
//Create UITabBarController
UITabBarController *theTabBarController = [[UITabBarController alloc] init];
NSArray *viewControllers = [NSSArry arrayWithObjects: fvc, svc, nil];
[theTabBarController setViewControllers:viewControllers];
// Create UINavigationController
UINavigationController *theNavigationController = [[UINavigationController
alloc]initWithRootViewController:theTabBarController];
[[self window] setRootViewController:theNavigationController];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
Then in the First View Controller i do a push to a second view
- (IBAction)Page2:(id)sender {
SBHomePageDetailViewController *detailPageViewController = [[SBHomePageDetailViewController alloc] init];
// Pushing to the stack
[[self navigationController] pushViewController:detailPageViewController animated:YES];
}
Now my UI shows the second view, however, the UITabBarController is missing. When i navigate back the tab bar view is back. How do I keep the tab bar controller visible in all ui screens?
Into AppDelegate.h file make property of theTabBarController:
#property (nonatomic, strong) UITabBarController *theTabBarController;
And here how I changed your didFinishLaunchingWithOptions method:
- (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.
FirstViewController *fvc = [[FirstViewController alloc] init];
SecondtViewController *svc = [[SecondtViewController alloc] init];
// Create UINavigationController
UINavigationController *theNavigationController = [[UINavigationController
alloc]initWithRootViewController:fvc];
//Create UITabBarController
self.theTabBarController = [[UITabBarController alloc] init];
NSArray *viewControllers = [NSArray arrayWithObjects: theNavigationController, svc, nil];
[self.theTabBarController setViewControllers:viewControllers];
[[self window] setRootViewController:theNavigationController];
[[self window] addSubview:self.theTabBarController.view];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
The problem in the code is that it tries to initialize a UITabBarController as the rootViewController of a UINavigationController in this line:
// Create UINavigationController
UINavigationController *theNavigationController = [[UINavigationController alloc]initWithRootViewController:theTabBarController];
From the docs:
rootViewController:
The view controller that resides at the bottom of the navigation stack. This object cannot be an instance of the UITabBarController class.
Try removing that line and, per #rmaddy's suggestion, put each View Controller in a Navigation Controller. Then set those Navigation Controllers as the Tab Bar Controller's VCs and set the App's RootViewController to the Tab Bar Controller:
FirstViewController *fvc = [[FirstViewController alloc] init];
SecondtViewController *svc = [[SecondtViewController alloc] init];
// Create the first UINavigationController
UINavigationController *firstNavigationController = [[UINavigationController
alloc]initWithRootViewController:fvc];
// Create the second UINavigationController
UINavigationController *secondNavigationController = [[UINavigationController
alloc]initWithRootViewController:svc];
//Create UITabBarController
theTabBarController = [[UITabBarController alloc] init];
NSArray *viewControllers = [NSArray arrayWithObjects: firstNavigationController, secondNavigationController, nil];
[theTabBarController setViewControllers:viewControllers];
[[self window] setRootViewController: theTabBarController];

how to create tab bar controller in a view controller not in app delegate? IOS 4.1

I am creating a tab bar application, but i want to create tab bar controller not in Main.xib by using interface builder. Because my app has no Main.Xib. So I either should do it in ViewController.xib or programmatically in a controller/appdelegate. I couldnt find any good tutorial or example for it.
In my app i have
AppDelegate.h
AppDelegate.m
ViewController.h
ViewController.m
ViewController.xib
My application starts with view of ViewController.xib know i want to add not a tab bar but a tab bar controller which will always stay bottom of views. How can i do that?
appdelagete.h
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
self.viewController = [[[ViewController alloc] initWithNibName:#"ViewController" bundle:nil] autorelease];
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
}
I have tried to do it programatically by looking at apple developer docs but couldnt figure it out.
Thanks in advance for any example code
I found this working great
appdelegate.h
#property (nonatomic, retain) UITabBarController *rootController
appdelegate.m
UIViewController *viewController1 = [[[ViewController alloc] initWithNibName:#"ViewController" bundle:nil] autorelease];
self.rootController = [[[UITabBarController alloc] init] autorelease];
self.rootController.viewControllers = [NSArray arrayWithObjects:viewController1, nil];
self.window.rootViewController = self.rootController;
This will create a tab bar with 3 views
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:view1, view2, view2, nil];
self.window.rootViewController = self.tabBarController;
///AppDelegate.m file : didFinishLaunchingWithOptions method
//initiate window
window = [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
//initiate viewcontrollers
FirstViewController *aFirstViewController = [FirstViewController new];
SecondViewController *aSecondViewController = [SecondViewController new];
aFirstViewController.tabBarItem.title = #"First";
aFirstViewController.tabBarIte
aSecondViewController.tabBarItem.title = #"Second";
gTabBarController = [[UITabBarController alloc]init];
gTabBarController.viewControllers = #[aFirstViewController ,aSecondViewController];
//show the main window and also make it key
[window makeKeyAndVisible];
window.rootViewController = gTabBarController;

How to fill the screen with DetailViewController

I created a project using Master-Detail Application template but i don't need a MasterView. So i deleted Masterview files and codes but this time when i rotate the Simulator/Device in the left side of my main screen a black area stays. I want to stretch my Detail view to fill all the scren but i have no idea how to do. Can anyone please help? Thanks in advance
Note: My app needen NavigationController, firstly i tried SingleView template, but i couldn't push views on that template so i created my app in Master-Detail Application template..
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
BNT_DetailViewController *detailViewController = [[[BNT_DetailViewController alloc] initWithNibName:#"BNT_DetailViewController" bundle:nil] autorelease];
UINavigationController *detailNavigationController = [[[UINavigationController alloc] initWithRootViewController:detailViewController] autorelease];
self.splitViewController = [[[UISplitViewController alloc] init] autorelease];
self.splitViewController.delegate = detailViewController;
self.splitViewController.viewControllers = [NSArray arrayWithObjects:detailNavigationController, nil];
self.window.rootViewController = self.splitViewController;
[self.window makeKeyAndVisible];
BNT_DetailViewController *detailViewController = [[[BNT_DetailViewController alloc] initWithNibName:#"BNT_DetailViewController" bundle:nil] autorelease];
navigationController = [[[UINavigationController alloc] initWithRootViewController:detailViewController] autorelease];
self.window.rootViewController = self.navigationController;
[self.window makeKeyAndVisible];
return YES;
}
This was the first appearance of the didFinishLaunchingWithOptions:of my ..AppDelegate.m but i changed it with below:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
//define navigationController in ..AppDelegate.h
BNT_DetailViewController *detailViewController = [[[BNT_DetailViewController alloc] initWithNibName:#"BNT_DetailViewController" bundle:nil] autorelease];
navigationController = [[[UINavigationController alloc] initWithRootViewController:detailViewController] autorelease];
self.window.rootViewController = self.navigationController;
[self.window makeKeyAndVisible];
return YES;
}
I answered my own answer in case it may be helpful for someone who lives the same pains:)

How to add a UITableViewController to a UINavigationController that is on a UITabViewController in XCode4?

I was following this tutorial on http://www.youtube.com/watch?v=LBnPfAtswgw and was able to replicate that in XCode 3 however with XCode 4 I am having some issues.
My app's root view controller is a UITabBarController on which I selected one of the tabs and selected a UINavigationController and then dragged a view as the tutorial suggests but am unable to select my UITableViewController class?
I also do not see a section where it says to select a tab bar controller and select the UINavigation controller (around 9:08)
Can anyone guide me as to what I am doing wrong?
You can do the same by using the following code:
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
TablelViewController *viewController1 = [[TablelViewController alloc] initWithNibName:#"TablelViewController" bundle:nil];
UINavigationController *navigationController1 = [[UINavigationController alloc] initWithRootViewController:viewController1];
TableViewController2 *viewController2 = [[TableViewController2 alloc] initWithNibName:#"TableViewController2" bundle:nil];
UINavigationController *navigationController2 = [[UINavigationController alloc] initWithRootViewController:viewController2];
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:navigationController1, navigationController2, nil];
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
This code goes in:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions