Single View to tab bar view - iphone

How to go into a tab based view from a Single view , CAn any one give me refrence for creating a tab based application programmatically from single view ?

you need to allocate and initialize the UITabeBarController and add it on your view controller/ App Delegate.

You can do one of the two:
Go to the AppDelegate.m and change the navigation controller to a TabBarController. Then you can add to the tab bar any view you like
Create a new DEMO application based on tabBar (attached) then you can see how the delegate works and copy the code you need.

Firsts Create all object of UIViewController and UINavigationController in AppDelegate.h file and use following method of AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window=[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds ]];
self.viewCon=[[ViewController alloc] init];
self.navCon=[[UINavigationController alloc] initWithRootViewController:self.viewCon];
self.navCon.navigationBar.tintColor=[UIColor blackColor];
self.viewCon.title=#"First View";
self.fView=[[FirstViewController alloc] init];
self.FnavCon=[[UINavigationController alloc] initWithRootViewController:self.fView];
self.FnavCon.navigationBar.tintColor=[UIColor blackColor];
self.fView.title=#"Secound View";
self.sView=[[SecoundViewController alloc] init];
self.SnavCon=[[UINavigationController alloc] initWithRootViewController:self.sView];
self.SnavCon.navigationBar.tintColor=[UIColor blackColor];
self.sView.title=#"Third View";
.
.
// create UIViewController and UINavigationController As you need
.
.
.
UIImage *img1=[UIImage imageNamed:#"Australia.gif"];
self.tbItem1=[[UITabBarItem alloc] initWithTitle:#"First Page" image:img1 tag:1];
self.viewCon.tabBarItem=self.tbItem1;
UIImage *img2=[UIImage imageNamed:#"Cameroon.gif"];
self.tbItem2=[[UITabBarItem alloc] initWithTitle:#"Secound Page" image:img2 tag:2];
self.fView.tabBarItem=self.tbItem2;
UIImage *img3=[UIImage imageNamed:#"Canada.png"];
self.tbItem3=[[UITabBarItem alloc] initWithTitle:#"Third Page" image:img3 tag:3];
self.sView.tabBarItem=self.tbItem3;
NSMutableArray *viewArr=[[NSMutableArray alloc] init];
[viewArr addObject:self.navCon];
[viewArr addObject:self.FnavCon];
[viewArr addObject:self.SnavCon];
self.tbCon=[[UITabBarController alloc] init];
self.tbCon.viewControllers=viewArr;
[self.window addSubview:tbCon.view];
[self.window makeKeyAndVisible];
return YES;
}
My Code is basic demo of how to create TabBar and how to add ViewController, this might be helpful for you.
Thanks :)

You can create tabbar application from single view based application. Go to xib add UITabbar from objects and add delegates of that. If you did this, you have to put tabbar in every view of tabbar section. It's simple but increase repetitive code.

Related

Switch from ViewController to TabBarController

I have one small problem. I have iOS app in xcode and when I launch it, it comes with
TabBarController. But then, I need to go to another ViewController (there would be some
info with pictures) and after that, I need go back to main page with TabbarController, but
when I click to back button, It show up without Tabbar on the bottom... For more clear, I've made a scheme...
Click to this link to show image scheme
Can anybody slove this please? Im working without storyboards, so I need it
programmatically. Thank you for every reply!
Steve
My guess is that your window.rootViewController is actually your UINavigationController. If you want the TabBar be present in all the screens then you need to make it your window.rootViewController.
Set it in your app delegate:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
[[NSBundle mainBundle] loadNibNamed:#"TabBarController" owner:self options:nil];
self.window.rootViewController = tbc;
return YES;
}
Create a xib with a tabcontroller, drop a navigation controller inside tab bar.
Set the Class of the Viewcontroller and the nib name.
Inside the method of the Button, needs to be like:
- (IBAction)go:(id)sender
{
Primeiro2ViewController *p2vc = [[Primeiro2ViewController alloc] initWithNibName:#"Primeiro2ViewController" bundle:nil];
p2vc.title = #"Primeiro 2";
[self.navigationController pushViewController:p2vc animated:YES];
self.navigationController.navigationBar.tintColor = [UIColor greenColor];
}
If you need a sample, I upload for you later.

how to add tab icons and titles to the tab items in split view

I'm implementing a split view based application.
I have 3 tabs for 3 root views in my application at the bottom of the left view / root views. For this I added three view controllers to the tabbar in app delegete.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
RootViewController *tab1 = [[RootViewController alloc] init];
DashboardViewController *tab2=[[DashboardViewController alloc] initWithNibName:#"DashboardViewController" bundle:nil];
SendUpdatesViewController *tab3=[[SendUpdatesViewController alloc] initWithNibName:#"SendUpdatesViewController" bundle:nil];
NSArray *tabViewArray=[[NSArray alloc] initWithObjects:tab1,tab2,tab3,tabBar, nil];
tabBar=[[UITabBarController alloc] init];
[tabBar setViewControllers:tabViewArray];
self.splitViewController.viewControllers = [NSArray arrayWithObjects:tabBar,_detailViewController, nil];
self.window.rootViewController = self.splitViewController;
[self.window makeKeyAndVisible];
return YES;
}
Now I need to add titles icons and the corresponding actions for these tabs.
You can assign titles to your viewcontrollers using the title-property. By default these titles are shown in the tabbar. You can also customize the UITabbarItem for each controller. Read more about that here: http://developer.apple.com/library/ios/DOCUMENTATION/UIKit/Reference/UITabBarItem_Class/Reference/Reference.html#//apple_ref/occ/cl/UITabBarItem
A good solution for this would be to overwrite the getters inside your viewcontrollers for the "title" and "tabBarItem" properties, making use of lazy instantiation to make sure the properties are properly set when they are accessed for the first time. This way you also keep this code contained in each viewcontroller's implementation.

rootViewController property does not work on iOS 5 project with xcode 4.2

Hi I just started experimenting on iOS 5. I created a project without storyboard and trying to add views programmatically (no use of interface builder at all). I have following code but rootViewController property of the window does not seem to work. I did NSLog on self.tabController and it shows me value(not null) but on the other side when after self.window.rootViewController = self.tabController, i output self.window.rootViewController it gives me null in console.
I have been struggling with this issue for a long time now. Any help would be appreciated.
Following is my didFinishLaunching method:
self.dataSource = [[[ADJWebDataSource alloc] init] autorelease];
ADJBrowseListingsViewController *browseListingsVC = [[ADJBrowseListingsViewController alloc] init];
ADJSecondViewController *secondVC = [[ADJSecondViewController alloc] init];
tabBarController = [[UITabBarController alloc] init];
tabBarController.view.frame = CGRectMake(0, 0, 320, 460);
navController = [[UINavigationController alloc] initWithRootViewController:browseListingsVC];
NSMutableArray* viewControllers = [[NSMutableArray alloc] initWithCapacity:2];
[viewControllers addObject:browseListingsVC];
[viewControllers addObject:secondVC];
[navController release];
[browseListingsVC release];
[secondVC release];
tabBarController.viewControllers = viewControllers;
[viewControllers release];
browseListingsVC.dataSource = self.dataSource;
NSLog(#"controller %#", self.tabBarController);
self.window.rootViewController = self.tabBarController;
NSLog(#"controller1 %#", self.window.rootViewController);
[self.window makeKeyAndVisible];
return YES;
Thanks
Vik
When you are using story board, why are you still creating objects for view controllers?
You can directly prepare the flow of your views in story board, add necessary segues etc.
If your view controller is floating (without any segues), you have to use the method "instantiateViewControllerWithIdentifier" in story board class.
For a view controller if you want to add navigation in story board, select the view controller, go to menu "Editor"->"Embed in" and select navigation controller. It will add navigation controller to your view controller.
Figured it out with the help of Firoze. Actually, I had to allocate and initialize self.window programmatically. I was confused as I never had to do that in iOS 4 or earlier. But then I just realized prior to iOS5, every project has a MainWindow.xib which had self.window allocated and initialized, now if I am not using storyboard in iOS5, there is no .xib file, I needed to allocate and initialize it myself in the code

UIActivityIndicatorView is not working (getting Facebook permissions affecting UIActivityIndicatorView)

I have a tabbarcontroller with three tabs/viewcontrollers.
When I first start my app, with my ActivityIndicator set to be visible and animated - courtesy of interface builder - it works fine.
However when I click a button an internet window opens to Facebook in order to get the user's permission.
Once the Facebook part is taken care it returns to my app but the ActivityIndicator is not longer animated - it is still visible though, just frozen.
If I switch to another tab/viewcontroller and then come back to the tab/viewcontroller with the ActivityIndicator everything works fine.
Is there a way to refresh my ViewController so that I don't have to programmatically make the ViewController switch back and forth? Or any other suggestions?
/* I searched the forums and I saw a similar question. It appeared that there was a broken connection. Therefore I'll include the code where I add the ViewController (i.e., "controller" to my tabbarcontroller). */
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
controller = [[DemoAppViewController alloc] init];
controller.view.frame = CGRectMake(0, 20, 320, 460);
controller.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"movieAppBackground.jpg"]];
MyTabBarViewController *vc2 = [[MyTabBarViewController alloc] init];
SecondViewController *vc3 = [[SecondViewController alloc] init];
controller.title = #"Intro Screen";
vc2.title = #"Explore";
vc3.title = #"Send a Pic";
UITabBarController *tbc = [[UITabBarController alloc] init];
tbc.viewControllers = [NSArray arrayWithObjects:controller, vc2, vc3, nil];
self.theTBC=tbc;
[controller release];
[vc2 release];
[vc3 release];
[tbc release];
[self.window addSubview:tbc.view];
[self.window makeKeyAndVisible];
return YES;
}
whereever u have used NIB file to show with viewcontrollers u have to create them with initwithname
Example
SecondViewController *r=[[SecondViewController alloc]initWithNibName:#"SecondViewController" bundle:nil];
like this change whereever u have used nib file to create instance,
i meaned for all custom viewcontrollers u have created with NIB file

Loading a view controller from viewDidLoad

Hello I want to create a tabbar application in iPhone using X-code.I have one splash screen and then after that I have my Home Screen where I want to create tabbar in that view. so, I create a tabbar in my ViewDidLoad method.
- (void)viewDidLoad {
NSLog(#"in Home");
tabBarController = [[UITabBarController alloc] init];
homeViewController = [[HomeViewController alloc]init];
NextViewController = [[NextViewController alloc]init];
tabBarController.viewControllers = [NSArray arrayWithObjects:homeViewController,nextViewController,nil];
window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
[window addSubview:tabBarController.view];
[window makeKeyAndVisible];
[super viewDidLoad];
}
Now when I compile & Run, It will call the HomeView again & again and when I tap on 2nd tab it will not call the NextView.
So, what is wrong in this code Plz help me..
Aren't you adding the homeViewController again and again since viewDidLoad is in homeViewController...
Can I recommend that you start a new project from the Tab Bar Application template just to look at how it's done when it's done right? You might even keep that and retrofit your splash screen into it.