How can i take a UITabbar in a viewController? - iphone

If i go straight to the point then my problem is i have an UIViewController with two button.
Button One --(onclick must navigate to)--->UITabbarController with 4 UITabbar item or i can take a UITabbar in my new UIViewController;
Button Two --(OnClick must navigate to)--->UITabbarController where first tabbaritem will contain containing UITableView or or i can take a UITabbar in my new UIViewController where first TabbarItem will contain a UITableView.
How can i make this??? sample code will more helpful neither suggestion. if you need any clarification please do not hesitate to ask.
With Best Regards
Emon

First, you need to have a NavigationController if you want another view controller to be pushed on when you click a button. The steps you would need to do are the following
Start with a MasterDetail app on iPhone
In the xib file, put your buttons and wire them up to the correct methods
In the view did appear of the view controller have the following code
self.tbc1 = [[UITabBarController alloc] init];
self.tbc2 = [[UITabBarController alloc] init];
UIViewController *tbc1vc1 = [[UIViewController alloc] init];
UIViewController *tbc1vc2 = [[UIViewController alloc] init];
UIViewController *tbc1vc3 = [[UIViewController alloc] init];
UITableViewController *tbc2vc1 = [[UITableViewController alloc] init];
UIViewController *tbc2vc2 = [[UIViewController alloc] init];
UIViewController *tbc2vc3 = [[UIViewController alloc] init];
[self.tbc1 setViewControllers:[NSArray arrayWithObjects:tbc1vc1,tbc1vc2,tbc1vc3, nil]];
[self.tbc1 setViewControllers:[NSArray arrayWithObjects:tbc2vc1,tbc2vc2,tbc2vc3, nil]];
Ofcourse you have to have properties defined for tbc1 adn tbc2 in your view controller.
In the app delegate do the following
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
ViewController *myViewController = [[ViewController alloc] init];
self.viewController = [[UINavigationController alloc] initWithRootViewController:myViewController];
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
Change the UIViewController in the .h file to UINavigationController like
#property (strong, nonatomic) UINavigationController *viewController;
Have the two buttons and wire it to the following methods
-(IBAction)tbc1Clicked{
[self.navigationController pushViewController:self.tbc1 animated:YES];
}
-(IBAction)tbc2Clicked{
[self.navigationController pushViewController:self.tbc2 animated:YES];
}

Related

Programmatically creating tab bar for ViewController

I've been looking at programatically adding a tab bar to my view controller because having a scroll view I can't place it on without it being in the middle of my view. I'm abit confused about how to add it. Does it need to be initiated in my ViewDidLoad method or my AppDelegate?
If I have:
UITabBarController *tabBar = [[UITabBarController alloc] init];
[self.view addSubview:tabBar.view];
[tabBar release];
How can I allocate it to the bottom of my scrollview?
Thanks!
Now in my appDelegate class :
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.viewController = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
UITabBarController *tabBarController = [[UITabBarController alloc] init];
ViewController* vc = [[ViewController alloc] init];
NSArray* controllers = [NSArray arrayWithObjects:vc, tabBarController, nil];
tabBarController.viewControllers = controllers;
[_window addSubview:tabBarController.view];
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
}
It's crashing and not sure if it's a release I'm missing.
Edit:
For anyone else wondering this in Appdelegate.m:
self.tabBarController = [[[UITabBarController alloc] init] autorelease];
self.tabBarController.viewControllers = #[viewController1, viewController2, viewController3, viewController4];
Take a look at this Documentation given by Apple: ViewControllers.
Sample Code :
- (void)applicationDidFinishLaunching:(UIApplication *)application {
UITabBarController *tabBarController = [[UITabBarController alloc] init];
FirstViewController* vc1 = [[FirstViewController alloc] init];
SecondViewController* vc2 = [[SecondViewController alloc] init];
NSArray* controllers = [NSArray arrayWithObjects:vc1, vc2, nil];
tabBarController.viewControllers = controllers;
// Add the tab bar controller's current view as a subview of the window
[window addSubview:tabBarController.view];
}
I did it this way. I copied this from my App Delegate and it works fine. Basically you add the view controllers to the tab bar then add the tab bar to the window's subview.
Instantiate the instance
iVacationTabBar = [[UITabBarController alloc] init];
However you create the views/view controllers:
UINavigationController *rvc_tools = [[UINavigationController alloc] initWithRootViewController: vc_tools];
UINavigationController *rvc_settings = [[UINavigationController alloc] initWithRootViewController: vc_settings];
UINavigationController *rvc_about = [[UINavigationController alloc] initWithRootViewController: vc_about];
UINavigationController *rvc_currentpage = [[UINavigationController alloc] initWithRootViewController: vc_currentpage];
UINavigationController *rvc_backup = [[UINavigationController alloc] initWithRootViewController:vc_backup];
Add the controllers to the array:
NSArray* controllers = [NSArray arrayWithObjects: rvc_currentpage, rvc_tools,rvc_settings, rvc_backup, rvc_about, nil];
Set the array of view controllers to your tab bar:
[iVacationTabBar setViewControllers: controllers animated:NO];
Add the tab bar to the window's subview.
[_window addSubview:iVacationTabBar.view];
You can not add a UITabbarController on a UIViewController. Instead, on a ViewController you have to show the TabbarController, create a TabbarController, set ViewControllers for it, and add it to a Window.

Tabbar in Second View

I have an activation page in my app, which is mandatory for every user to activate the app.
Once the app is activated, the user moves to the tabbed bar view.
I have created a tabbed bar application, where from my activationView on click of button I am trying to call the tab bar, I am getting an entire black screen.
- (IBAction)moveToActivateView:(id)sender {
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
UITabBarController *tabBarController = [[UITabBarController alloc]init];
[self.view addSubview:tabBarController.view];
appDelegate.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
UIViewController *viewController1 = [[FirstViewController alloc] initWithNibName:#"FirstViewController" bundle:nil];
UIViewController *viewController2 = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
self.tabBarController.viewControllers = #[viewController1, viewController2];
appDelegate.window.rootViewController = self.tabBarController;
[appDelegate.window makeKeyAndVisible];}
Hey make your tabBarController's property in appDelegate and assign all ViewController there then call your tabBarController from yourViewController
in AppDelegate.h
#property (nonatomic, retain) IBOutlet UINavigationController *navigationController;
#property (retain, nonatomic) IBOutlet UITabBarController *tabBarController;
in AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[self makeTabBar];
[self addInitialVIew];
[self.window makeKeyAndVisible];
return YES;
}
-(void)makeTabBar
{
tabBarController = [[UITabBarController alloc] init];
tabBarController.delegate=self;
FirstViewController *firstVC =[[FirstViewController alloc] initWithNibName:#"FirstViewController" bundle:nil];
UINavigationController *firstNC = [[UINavigationController alloc] initWithRootViewController:firstVC];
firstNC.tabBarItem.title=#"Profile";
firstVC.tabBarController.tabBar.tag = 0;
SecondViewController *secondVC = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
UINavigationController *SecondNavController = [[UINavigationController alloc] initWithRootViewController:secondVC];
SecondNavController.tabBarItem.title = #"Search";
secondVC.tabBarController.tabBar.tag = 1;
NSArray *viewControllers =[[NSArray alloc]initWithObjects:firstNC,SecondNavController, nil];
[tabBarController setViewControllers:viewControllers animated:NO];
}
-(void) addInitialVIew
{
InitialViewController *initialViewController = [[InitialViewController alloc]initWithNibName:#"InitialViewController" bundle:nil];
navigationController = [[UINavigationController alloc]initWithRootViewController:ViewController];
[self.window addSubview:navigationController.view];
}
Now in InitialViewController you can add yourTabBar and remove InitialViewController
- (IBAction)switchToTabBarBtnPress:(id)sender
{
AppDelegate *appdelegte =(AppDelegate*)[[UIApplication sharedApplication]delegate];
[[[appdelegte navigationController] view]removeFromSuperview];
[[appdelegte window]addSubview:[[appdelegte tabBarController]view]];
[[appdelegte tabBarController]setSelectedIndex:0];
}
And Follow my answer
Answer
You want to create dynamic tabbar application to resolve your problem. So you just create the view based application. in the view controller viewdidload method put these code
tabbar1 = [[UITabBarController alloc] init];
artist_tab_obj = [[artist_tab alloc] initWithNibName:#"artist_tab" bundle:nil];
UINavigationController *tabItem1 = [[[UINavigationController alloc] initWithRootViewController: artist_tab_obj] autorelease];
tabItem1.title=#"Artist";
tabItem1.tabBarItem.image=[UIImage imageNamed:#"Icon1.png"];
music_tab_obj = [[music_tab alloc] initWithNibName:#"music_tab" bundle:nil];
UINavigationController *tabItem2 = [[[UINavigationController alloc] initWithRootViewController: music_tab_obj] autorelease];
tabItem2.title=#"Music";
tabItem2.tabBarItem.image=[UIImage imageNamed:#"Icon2.png"];
shout_tab_obj = [[shout_tab alloc] initWithNibName:#"shout_tab" bundle:nil];
UINavigationController *tabItem3 = [[[UINavigationController alloc] initWithRootViewController: shout_tab_obj] autorelease];
tabItem3.title=#"Shout";
tabItem3.tabBarItem.image=[UIImage imageNamed:#"Icon3.png"];
schedule_tab_obj = [[schedule_tab alloc] initWithNibName:#"schedule_tab" bundle:nil];
UINavigationController *tabItem4 = [[[UINavigationController alloc] initWithRootViewController: schedule_tab_obj] autorelease];
tabItem4.title=#"Schedule";
tabItem4.tabBarItem.image=[UIImage imageNamed:#"Icon4.png"];
follow_tab_obj = [[follow_tab alloc] initWithNibName:#"follow_tab" bundle:nil];
UINavigationController *tabItem5 = [[[UINavigationController alloc] initWithRootViewController: follow_tab_obj] autorelease];
tabItem5.title=#"Follower";
tabItem5.tabBarItem.image=[UIImage imageNamed:#"Icon5.png"];
tabbar1.viewControllers = [NSArray arrayWithObjects:tabItem1, tabItem2,tabItem3,tabItem4,tabItem5,nil];
In user acceptation is yes button action call this code.
[self.view insertSubview:tabbar1.view belowSubview: artist_tab_obj.view];
tabbar1.selectedIndex=1;
[self presentModalViewController:tabbar1 animated:YES];
Did you realize that your local varialbe tabBarController is not identical but sort of hides the property self.tabBarController? You create a new UITabBarCotnroller and assign it to your local variable tabBarController, which is accessible from this method only. Then you manipulate (add newly created view controllers) etc to self.tabBarController. Self.tabBarController could easily be nil here or anything else. But it is not the very UITabBarController that you just created.
And it is self.tabBarController (so probably nil) what you assign to the window as rootViewController.
You do add the tabBarController's view as subview to self.view. Besides that I do not know what self acutally is, I don't think is it really nessessary to manually add the tabBar's view as subview. Setting the root view controller (properly) should be enough

How to add a tab bar to flipside view only

I already have the basic code for a utility application, but when the application flips to the second view I want to add a tabbarItem to the bottom of the flipped view only, if the view is flipped back to the original view the tab bar shouldn't show up. How can I add this feature, I'm using simulator 4.1 by the way, thanks!
Here's the code that shows the flipped side when the button is clicked, I want it to flip to a tab bar controller instead.
- (IBAction)showInfo:(id)sender {
FlipsideViewController *controller = [[FlipsideViewController alloc] initWithNibName:#"FlipsideView" bundle:nil];
controller.delegate = self;
controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:controller animated:YES];
[controller release];
}
You can hide the UITabbar by using the following code
[yourTabBar setHidden :YES];
and show it using the following code
[yourTabBar setHidden:NO];
By Show/Setup do you mean adding the tabbar controller on the view??Well i am assuming that..You can Add the tabbar controller as a rootviewcontroller of your window i.e. your AppDelegate.Here is the sample Code:
UIViewController *viewController1 = [[FirstViewController alloc] initWithNibName:#"FirstViewController" bundle:nil];
UINavigationController *navCon1=[[UINavigationController alloc] initWithRootViewController:viewController1];
UIViewController *viewController2 = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
UINavigationController *navCon2=[[UINavigationController alloc] initWithRootViewController:viewController2];
UIViewController *viewController3=[[ThirdViewController alloc] initWithNibName:#"ThirdViewController" bundle:nil];
UINavigationController *navCon3=[[UINavigationController alloc] initWithRootViewController:viewController3];
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:navCon1, navCon2,navCon3, nil];
self.window.rootViewController = self.tabBarController;
You're switching between two UIViewControllers, right? Let the second view controller, the one that appears after flipping, be a UITabBarViewController.

In iPhone App ,How to add tab bar controller on some particular view controller Programmatically?

In iPhone App ,How to add tab bar controller on some particular view controller Programmatically?
Here the viewcontroller Class is is UITableviewcontroller .
right now if i am adding the tab bar it appears some where in table view. i want to display it on bottom of window and and tableview should scroll separately from Tabbarcontroller
Please help and suggest
Thanks
FirstView *view1 = [[FirstView alloc]init];
SecondView *view2 = [[SecondView alloc] init];
UINavigationController *firstview = [[UINavigationController alloc] initWithRootViewController:view1];
UINavigationController *secondview = [[UINavigationController alloc] initWithRootViewController:view2];
UITabBarController* tabBar = [[UITabBarController alloc] init];
[tabBar setViewControllers:[NSArray arrayWithObjects:view1, view2, nil]];
[tabBar setDelegate:self];
UINavigationController* navigationController = [[UINavigationController alloc] initWithRootViewController:tabBar];
[self.view addSubview:navigationController.view];
Try this. here FirstView, SecondView are the class. if you click the tab, corresponding class (view) will open. And you have declare the tabbar and navigation controller delegate.
first you just select tabbar base application from project template.
now double click on mainwindow.xib its open it in interface builder.
now click on tabbarcontroller and open inspector window.
in above image your are able to see list of viewcontrollers. click on class tab will gives you the option of controller type. see in below image
if your are selecting table view controller then your first controller is table view. and tabbar is not in your view. table is scroll separately.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
//************ WRITE THE CODE OVER HERE FOR THE CREATION OF THE TAB BAR BY CODING**************
tabbar_object=[[UITabBarController alloc] init];
self.tabbarViewController=[[[TabBarViewController alloc] initWithNibName:#"TabBarViewController" bundle:nil] autorelease];
self.viewcontroller1=[[[FirstViewControllerForTabBar alloc] initWithNibName:#"FirstViewControllerForTabBar" bundle:nil] autorelease];
self.viewcontroller2=[[[SecondViewControllerForTabBar alloc] initWithNibName:#"SecondViewControllerForTabBar" bundle:nil] autorelease];
UINavigationController *tabbatnavigation=[[UINavigationController alloc] initWithRootViewController:tabbarViewController];
tabbatnavigation.title=#"First Tab";
UINavigationController *navigation1=[[UINavigationController alloc] initWithRootViewController:viewcontroller1];
navigation1.title=#"Sign Up";
UINavigationController *navigation2=[[UINavigationController alloc] initWithRootViewController:viewcontroller2];
navigation2.title=#"Sign Out";
tabbar_object.viewControllers=[[NSArray alloc] initWithObjects:navigation1,navigation2,tabbatnavigation, nil];
[tabbar_object setSelectedIndex:0];
//************************************************************************************************
self.window.rootViewController = tabbar_object;
[self.window makeKeyAndVisible];
return YES;
}

Complex multiview iphone ios

i need to implement a multiview app really complex to me and i need some advice. The multiview app is something like:
First view: Normal UIViewController with one button, when i push it go to second view
Second view(aka mainview): a Windows with Tab Bar with 2 tabbar item who switch between:
Second view A: Normal UIViewController with some elements
Second view B: UITableViewController
Can someone give me an advice where to start reading or some examples?
thx
my advice is to read sample code form apple there you can also find coding how to s so good luck, or you can find example codes all over the stack just search. for example navigation based app:
UINavigationController doesn't work in a the moreNavigationController of a UITabBarController
or simple transition:
SecondViewController *screen = [[SecondViewController alloc] initWithNibName:nil bundle:nil];
screen.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentModalViewController:screen animated:YES];
[screen release];
hope it helps bye
wblade
You need to start with view based application. And then create a UITabbarController in you appDelegate file.
Appdelegate.h
UITabBarController *tabBarController;
// set properties
Appdelegate.m
// Synthsize
tabBarController = [[UITabBarController alloc] init];
tabBarController.delegate=self;
//Adding Search,Nearby,Map,AboutUs,Favorites Tabs to tabBarController
Search * search = [[Search alloc] init];
UINavigationController *searchNav = [[UINavigationController alloc] initWithRootViewController:search];
Nearby* nearby = [[Nearby alloc] init];
UINavigationController *nearbyNav = [[UINavigationController alloc] initWithRootViewController:nearby];
Map* map = [[Map alloc] init];
UINavigationController *mapNav = [[UINavigationController alloc] initWithRootViewController:map];
AboutUs* aboutUs = [[AboutUs alloc] init];
UINavigationController *aboutUsNav = [[UINavigationController alloc] initWithRootViewController:aboutUs];
Favorites* favorites = [[Favorites alloc] init];
UINavigationController *favoritesNav = [[UINavigationController alloc] initWithRootViewController:favorites];
NSArray* controllers = [NSArray arrayWithObjects:searchNav,nearbyNav,mapNav,aboutUsNav,favoritesNav, nil];
tabBarController.viewControllers = controllers;
[window addSubview:tabBarController.view];
You can accordingly manage in which tab you want to place navigation controller or only a view controller.
Then in each of the view controllers mentioned above you need to implement
- (id)init {}
in which you can set Tab name and image.