Tab Bar view does not load - iphone

In my app delegate interface I have:
#interface pivcalc1AppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
IBOutlet UITabBarController *RootController;
}
#property (nonatomic, retain) IBOutlet UIWindow *Window;
#property (nonatomic, retain) IBOutlet UITabBarController *RootController;
in implementation, I have:
#synthesize Window;
#synthesize RootController;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions: (NSDictionary *)launchOptions
{
[Window addSubview:RootController.view];
// Override point for customization after application launch.
[self.Window makeKeyAndVisible];
return YES;
}
In my main xib window I have a tab bar controller which is connected to app delegate as rootController.
When I run the program, the window shows but the tab bar view does not get loaded.
Appreciate any help. Thanks.

Are you have a subViewController in RootController in xib? if you're not, It's code following.
UITabBarController should be have a subViewController.
UINavigationController *localNavigationController;
NSMutableArray *localViewControllerArray = [[NSMutableArray alloc] initWithCapacity:2];
SubViewController *subviewController0 = [[SubViewController alloc] init];
localNavigationController = [[UINavigationController alloc] initWithRootViewController:subviewController0];
[localViewControllerArray addObject:localNavigationController];
[subviewController0 release];
[localNavigationController release];
SubViewController *subviewController1 = [[SubViewController alloc] init];
localNavigationController = [[UINavigationController alloc] initWithRootViewController:subviewController1];
[localViewControllerArray addObject:localNavigationController];
[subviewController1 release];
[localNavigationController release];
RootController.viewControllers = localViewControllerArray;
[localViewControllerArray release];
[self.window addSubview:tabBarController.view];
[self.window makeKeyAndVisible];

Related

Tab Bar controller in iPhone

I have added Two Tabs in my app, to load two view controller using these tabs
Tab1 : Home
Tab2 : Favourite
so I have written below code to achieve this
In app Delegate
AppDelegate.h
#class ViewController,FavViewController;
#interface AppDelegate : UIResponder <UIApplicationDelegate>
#property (strong, nonatomic) UIWindow *window;
#property (strong, nonatomic) ViewController *viewController;
#property (strong, nonatomic) FavViewController *favViewController;
#property (strong, nonatomic) UITabBarController *tabBarController;
#end
AppDelegate.m
#implementation AppDelegate
#synthesize window = _window;
#synthesize viewController;
#synthesize favViewController;
#synthesize tabBarController;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
baseTabBarController = [[UITabBarController alloc]init];
baseTabBarController.delegate=self;
viewController = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
UINavigationController *homeView = [[UINavigationController alloc]initWithRootViewController:viewController];
favViewController = [[FavViewController alloc] initWithNibName:#"FavViewController" bundle:nil];
favViewController.title = #"My Favourite";
UINavigationController *favouriteView = [[UINavigationController alloc] initWithRootViewController:favViewController];
NSArray *controllers = [NSArray arrayWithObjects:homeView,favouriteView, nil];
baseTabBarController.viewControllers = controllers;
[self.window addSubview:baseTabBarController.view];
[self.window makeKeyAndVisible];
return YES;
}
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)selectedViewController
{
if (tabBarController.selectedIndex == 0) {
}else if (tabBarController.selectedIndex == 1) {
[(FavViewController *)[(UINavigationController*)selectedViewController topViewController] getData];
}else if (tabBarController.selectedIndex == 2) {
NSLog(#"2");
}
}
and here is the Result Screen I am getting
So Where I am having trouble in this..
If I switch to the next screen my First tab doesn't load First Screen (Home screen) instead just stay on current screen.
Let me try with example
There are four screens in my app let say A, B, C, D
I have added Tabs for A and C screens,those are available in whole app
(All screen).
Now if I'll start app and go A ->B->C -> D and press on Home Tab (A)
it doesn't load Screen A, instead just stay on Current screen
but good thing is if i do same process with another tab C (My
Favourite) it loads correct screen.
Edit: I have implemented didSelectViewController method as #sanjit suggested me but in that I not able distinguish, which tab is tapped this time?
I would be really appreciate!! if someone can point me on right direction
Use Tabbar delegate and using parameter instance of uiviewcontroller call poptorootviewcontroller method. Hope it may work for you. Try the following code
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
UINavigationController *navController = (UINavigationController*)tabBarController.selectedViewController;
[navController popToRootViewControllerAnimated:YES];
}
replace the code
[viewController setTabBarItem:tab1];
[favViewController setTabBarItem:tab2];
with
[[tabbarController tabBar] setItems:[NSArray arrayWithObjects:tab1,tab2, nil]];
that should resolve.
Viewcontroller.h
#property(nonatomic, retain) UITabBarItem *item1;
#property(nonatomic, retain) UITabBarItem *item2;
#property(nonatomic, retain) UITabBarItem *item3;
#property(nonatomic, retain) UITabBarItem *item4;
#property(nonatomic, retain) IBOutlet UITabBar *tabBar;
Viewcontroller.m
#synthesize item1,item2,item3,item4,tabBar;
- (void)viewDidLoad
{
item1 = [[UITabBarItem alloc] initWithTitle:#"Home" image:[UIImage imageNamed:#"home.png"] tag:0];
item2 = [[UITabBarItem alloc] initWithTitle:#"My Lists" image:[UIImage imageNamed:#"mylist"] tag:1];
item3 = [[UITabBarItem alloc] initWithTitle:#"Search" image:[UIImage imageNamed:#"search.png"] tag:2];
item4 = [[UITabBarItem alloc] initWithTitle:#"Settings" image:[UIImage imageNamed:#"setting.png"] tag:3];
NSArray *items = [NSArray arrayWithObjects:item1,item2,item3,item4, nil];
[tabBar setItems:items animated:NO];
[tabBar setSelectedItem:[tabBar.items objectAtIndex:2]];
tabBar.delegate=self;
[self.view addSubview:tabBar
];
}
/****Function Call******/
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
{
if(item.tag == 0)
{
Newsfeeds *obj = [[Newsfeeds alloc]initWithNibName:#"Newsfeeds" bundle:Nil];
[self.navigationController pushViewController:obj animated:NO];
}
else if(item.tag == 1)
{
MyLists *obj = [[MyLists alloc]initWithNibName:#"MyLists" bundle:Nil];
[self.navigationController pushViewController:obj animated:NO];
}
else if(item.tag == 3)
{
Settings *obj = [[Settings alloc]initWithNibName:#"Settings" bundle:Nil];
[self.navigationController pushViewController:obj animated:NO];
}
}

How to access method linked to UIBarButton on NavigationController

II wanted to add a UIBarButton to my UINavigationController. I did that with the help of following code,
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UIViewController *rootController = [[RootViewController alloc] initWithNibName:#"RootViewController" bundle:nil];
self.navigationController = [[UINavigationController alloc] initWithRootViewController:rootController];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[self.window addSubview:navigationController.view];
[self.window makeKeyAndVisible];
return YES;
}
- (void)viewDidLoad
{
[super viewDidLoad];
UIBarButtonItem *addInfoButton = [[UIBarButtonItem alloc] initWithTitle:#"Add Info" style:UIBarButtonItemStylePlain target:self action:#selector(addCustomerInfo)];
self.navigationItem.rightBarButtonItem = addInfoButton;
}
-(void) addCustomerInfo
{
AddInfoViewController *addVC = [[AddInfoViewController alloc] initWithNibName:#"AddInfoViewController" bundle:nil];
[addVC setModalTransitionStyle:UIModalTransitionStyleCoverVertical];
[self presentModalViewController:addVC animated:YES];
}
Should I declare "-(void) addCustomerInfo" in .h file? I already tried that but no luck.
The code still throws the exeption,
2012-08-06 04:16:22.200 TableView[5698:f803] -[RootViewController addCustomerInfo]: unrecognized selector sent to instance 0x6c662b0
2012-08-06 04:16:22.202 TableView[5698:f803] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[RootViewController addCustomerInfo]: unrecognized selector sent to instance 0x6c662b0'
The code for your class is correct. You need to change your App Delegate
I suggest creating a property in your App Delegate to store your navigation controller -#property (strong, nonatomic) UINavigationController *navController; - Don't forget to synthesize it.
Then, when you create your Navigation controller, set it to your property - self.navController = [[UINavigationController alloc] init];
This will guarantee your NavigationController is properly retained and that it can be accessed correctly by other classes in your application.
Below some sample code that might make it clearer:
First the AppDelegate header file:
//AppDelegate.h
#import <UIKit/UIKit.h>
#interface AppDelegate : UIResponder <UIApplicationDelegate>
{
UINavigationController *navController;
}
#property (strong, nonatomic) UIWindow *window;
#property (strong, nonatomic) UINavigationController *navController;
#end
And the implementation file:
// AppDelegate.m
#import "AppDelegate.h"
#implementation AppDelegate
#synthesize window = _window;
#synthesize navController;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.navController = [[UINavigationController alloc] init];
UIViewController *myVC= [[myVC alloc] init];
[self.navController pushViewController:myVC animated:NO];
[self.window addSubview:navController.view];
[self.window makeKeyAndVisible];
return YES;
}
Check out this tutorial:
http://www.innovatelabs.in/2010/03/implementing-uibarbuttonitem/
I'm not so fluent with iOS, but usually when you use a selector, your add : to it #selector(addCustomInfo:)
and then the function would look like this:
-(void) addCustomInfo:(UIBarButtonItem *)myButton {
NSLog(#"YOU CLICKED ME!");
}

I want to implement TabBarController and add Navigation Controller View but in vain

Iknow how to add a TabBar with two TabBar buttons on it by Interface Builder. And I link each button to a Navigation Controller.
Now I want to learn how to do everything programmatically. Now I can see a TabBar in simulator but with no buttons on it. Can someone please help me with this. Thanks!
Here's the TabBarAppDelegate.h.
#import <UIKit/UIKit.h>
#interface TabBarAppDelegate : NSObject <UIApplicationDelegate>
{
UIWindow *window;
UITabBarController *tabBarController;
IBOutlet UINavigationController *navigationController1;
IBOutlet UINavigationController *navigationController2;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) UITabBarController *tabBarController;
#property (nonatomic, retain) IBOutlet UINavigationController *navigationController1;
#property (nonatomic, retain) IBOutlet UINavigationController *navigationController2;
#end
Here's TabBarAppDelegate.m.
#import "TabBarAppDelegate.h"
#import "FirstViewController.h"
#import "SecondViewController.h"
#implementation TabBarAppDelegate
#synthesize window=window;
#synthesize tabBarController;
#synthesize navigationController1;
#synthesize navigationController2;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
tabBarController = [[UITabBarController alloc]init];
FirstViewController *firstViewController = [[FirstViewController alloc] initWithNibName:#"FirstViewController" bundle:nil];
firstViewController.title = #"First";
[self.navigationController1 pushViewController:firstViewController animated:NO];
SecondViewController *secondViewController = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
secondViewController.title = #"NavCal";
[self.navigationController2 pushViewController:secondViewController animated:NO];
tabBarController.viewControllers = [NSArray arrayWithObjects:navigationController1, navigationController2, nil];
[window addSubview:tabBarController.view];
[self.window makeKeyAndVisible];
[firstViewController release];
[secondViewController release];
return YES;
}
- (void)dealloc
{
[tabBarController release];
[window release];
[super dealloc];
}
It looks like you are not initializing the navigation view controllers. See if this would work:
TabBarAppDelegate.h
Remove the properties navigationController1 and navigationController2
TabBarAppDelegate.m
Replace
[self.navigationController1 pushViewController:firstViewController animated:NO];
with
UINavigationController *navigationController1 = [[UINavigationController alloc] initWithRootViewController:firstViewController];
Replace
[self.navigationController2 pushViewController:secondViewController animated:NO];
with
UINavigationController *navigationController2 = [[UINavigationController alloc] initWithRootViewController:secondViewController];
Release navigationController1 and navigationController2 after adding them to the tabBarController

iPhone: can we use Tab bar controller in view base application

can we use UITab bar controller in view base application thank you
Yes you can.
you can look at the "TheElements" example that apple provides.
you can find it here:
https://developer.apple.com/library/ios/#samplecode/TheElements/Introduction/Intro.html
look at the app delegate.
its very strait forward example.
for you request i tried to make a simple example:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[self setupPortraitUserInterface];
return YES;
}
- (UINavigationController *)AchievementsControllerWrappingViewController:(NSInteger*)tabIndex{
switch(tabIndex){
case 0:
FirstViewController *theViewController;
theViewController = [[FirstViewController alloc] init];
break;
case 1:
SecondViewController *theViewController;
theViewController = [[SecondViewController alloc] init];
break;
}
UINavigationController *theNavigationController;
theNavigationController = [[UINavigationController alloc] initWithRootViewController:theViewController];
[theViewController release];
return theNavigationController;
}
- (void)setupPortraitUserInterface {
UINavigationController *localNavigationController;
UIWindow *localWindow;
localWindow = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window = localWindow;
[localWindow release];
tabBarController = [[UITabBarController alloc] init];
NSMutableArray *localViewControllersArray = [[NSMutableArray alloc] initWithCapacity:2];
for(int i=0;i<2;i++){
localNavigationController = [self AchievementsControllerWrappingViewController:i];
[localViewControllersArray addObject:localNavigationController];
[localNavigationController release];
}
tabBarController.viewControllers = localViewControllersArray;
[localViewControllersArray release];
[window addSubview:tabBarController.view];
[window makeKeyAndVisible];
}
i am not next to xCode and i did it with text edit, so please check it when you use it.
shani
Yes we can..
For that you have to create UITabBarViewController and its Object then refrence it with you Application..like :
in your AppDelegate.h
#interface youAppDelegate.h : UIApplicationDelegate {
UIWindow *window;
YourViewController *viewController;
// Declare Your TabBarController Here
UITabBarController *tabBar;
}
#property (nonautomic, retain) IBOutlet UIWindow *window;
#property (nonautomic, retain) IBOutlet TabBarViewController *tabBar;
#end
in you implementation file's ApplicationDidFinish Launching add the following Code
viewController = [[YourViewController alloc] init];
[viewController addSubView:tabBar];
[self.window addSubView:viewController];
In interface builder you have to add the TabBarController to your MainWindow and relate it with the IBOutLet. Give what ever view you want to display in tabBar.
enjoy..

UITabBarController rotation problem with popViewControllerAnimated and selectedIndex (iPhone SDK)

This is a very important auto rotate issue and easy to reproduce.
My application has a UITabBarController. Each tab is a UINavigationController. Auto rotation is handled with normal calls to shouldAutorotateToInterfaceOrientation and didRotateFromInterfaceOrientation.
The interface rotates normally until I call UIViewController.popViewControllerAnimated and change UITabBarController.selectedIndex.
Steps to reproduce:
Create a demo Tab Bar Application.
Add the following code to the App Delegate .h file:#import <UIKit/UIKit.h>
#interface TestRotationAppDelegate : NSObject {
UIWindow *window;
UITabBarController *tabBarController;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) IBOutlet UITabBarController *tabBarController;
#end
// Redefine the interface to cach rotation messages
#interface UITabBarController (TestRotation1AppDelegate)
#end
Add the following code to the App Delegate .m file:#import "TestRotationAppDelegate.h"
#implementation TestRotationAppDelegate
#synthesize window;
#synthesize tabBarController;
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[window addSubview:tabBarController.view];
[window makeKeyAndVisible];
return YES;
}
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return YES;
}
-(void)dealloc {
[tabBarController release];
[window release];
[super dealloc];
}
#end
#implementation UITabBarController (TestRotation1AppDelegate)
-(void)viewDidLoad {
[super viewDidLoad];
// Add a third tab and push a view
UIViewController *view1 = [[[UIViewController alloc] init] autorelease];
view1.title = #"Third";
UINavigationController *nav = [[[UINavigationController alloc] initWithRootViewController:view1] autorelease];
NSMutableArray *array = [[NSMutableArray alloc] init];
[array addObjectsFromArray:self.viewControllers];
[array addObject:nav];
self.viewControllers = array;
// Push view2 inside the third tab
UIViewController *view2 = [[[UIViewController alloc] init] autorelease];
[nav pushViewController:view2 animated:YES];
// Create a button to pop view2
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(50, 50, 220, 38);
[button setTitle:#"Pop this view" forState:UIControlStateNormal];
[button addTarget:self action:#selector(doAction) forControlEvents:UIControlEventTouchUpInside];
[view2.view addSubview:button];
}
-(void) doAction {
// ROTATION PROBLEM BEGINS HERE
// Remove one line of code and the problem doesn't occur.
[self.selectedViewController popViewControllerAnimated:YES];
self.selectedIndex = 0;
}
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return YES;
}
#end
The interface auto rotates normally until you tap the button on tab #3.
Your help will be geatly appreciated!
iPhone SDK 3.2 solves this issue.
With previous SDK use [self.selectedViewController popViewControllerAnimated:NO].