custom tab bar controller not working? - iphone

I am trying to build a custom tab bar controller but for some reason the views will not switch... The initial view is loaded properly. Here is my init method:
- (id)initWithNibName:(NSString *)nibNameOrNil
bundle:(NSBundle *)nibBundleOrNil
{
AccountViewController *accountViewController = [[AccountViewController alloc]
initWithNibName:#"AccountViewController" bundle:nil];
MoreViewController *moreViewController = [[MoreViewController alloc]
initWithNibName:#"MoreViewController" bundle:nil];
BarTabViewController *barTabViewController = [[BarTabViewController alloc]
initWithNibName:#"BarTabViewController" bundle:nil];
LocationsViewController *locationsViewController = [[LocationsViewController alloc]
initWithNibName:#"LocationsViewController" bundle:nil];
self.viewControllers = [NSArray arrayWithObjects:locationsViewController, accountViewController,
barTabViewController, moreViewController, nil];
[self.view addSubview:locationsViewController.view];
self.selectedController = locationsViewController;
return self;
}
Like I said, this will display the selected controller properly, however when the app launches and I try to switch views with the tab bar, the subview just become grey... I have been looking through several tutorials to try to figure out this issue, but it seems that I am doing it exactly the same. I have also checked the IB file to make sure my tabs are connected properly, they are. The following is the code to switch items:
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
{
if (item == locationsTabBarItem) {
UIViewController *locationsController = [viewControllers objectAtIndex:0];
[self.selectedController.view removeFromSuperview];
[self.view addSubview:locationsController.view];
self.selectedController = locationsController;
}
else if (item == accountsTabBarItem) {
UIViewController *accountsController = [viewControllers objectAtIndex:1];
[self.selectedController.view removeFromSuperview];
[self.view addSubview:accountsController.view];
self.selectedController = accountsController;
}
else if (item == barTabTabBarItem) {
UIViewController *barTabController = [viewControllers objectAtIndex:2];
[self.selectedController.view removeFromSuperview];
[self.view addSubview:barTabController.view];
self.selectedController = barTabController;
}
else {
UIViewController *moreController = [viewControllers objectAtIndex:3];
[self.selectedController.view removeFromSuperview];
[self.view addSubview:moreController.view];
self.selectedController = moreController;
}
}

try With this one
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.selectedIndex = 0;
self.tabBarController.viewControllers = [NSArray arrayWithObjects:locationsViewController, accountViewController,
barTabViewController, moreViewController, nil];
self.tabBarController.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self.navigationController pushViewController:delegate.tabBarController animated:YES];

Related

Detect selected tabbar when switching tab bar

I have three tab bar application and my tabs is TAB1, TAB2 , TAB3 i have write the following code in TAB1 view controller to detect which tab user have pressed
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
{
NSLog(#"tab selected: %#", item.title);
}
But this delegate never get called
i have setup my tab in appdelegate.m something like that
- (void)setupTabBar
{
self.myWorkListViewController = [[MyWorkListViewController alloc] initWithNibName:#"MyWorkListViewController"
bundle:nil];
self.askHRViewController = [[AskHRViewController alloc] initWithNibName:#"AskHRViewController"
bundle:nil];
self.moreViewController = [[MoreViewController alloc] initWithNibName:#"MoreViewController"
bundle:nil];
self.bookLeaveViewController = [[BookLeaveViewController alloc] initWithNibName:#"BookLeaveViewController"
bundle:nil];
self.helpViewController = [[HelpViewController alloc] initWithNibName:#"HelpViewController"
bundle:nil];
// Create navigation controllers
workListNavigationController = [[UINavigationController alloc] initWithRootViewController:self.myWorkListViewController];
askHRNavigationController = [[UINavigationController alloc] initWithRootViewController:self.askHRViewController];
bookLeaveViewController = [[UINavigationController alloc] initWithRootViewController:self.bookLeaveViewController];
moreNavigationController = [[UINavigationController alloc] initWithRootViewController:self.moreViewController];
helpNavigationController = [[UINavigationController alloc] initWithRootViewController:self.helpViewController];
[self setTabBarImagesAndText];
// Setup tab bar controller
self.tabBarController = [[UITabBarController alloc] init];
[self.tabBarController setViewControllers:[NSArray arrayWithObjects:workListNavigationController, askHRNavigationController, bookLeaveViewController, helpNavigationController,moreNavigationController, nil]];
//Set TabBar background
[self.tabBarController.tabBar insertSubview:[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"TabBar_iOS4_Background"]] atIndex:0];
[self.tabBarController setSelectedIndex:0];
}
you can detect selected Tabbar Item like this way:-
as par your code you just need to add this line
// Setup tab bar controller
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.delegate=self;
[self.tabBarController setViewControllers:[NSArray arrayWithObjects:workListNavigationController, askHRNavigationController, bookLeaveViewController, helpNavigationController,moreNavigationController, nil]];
//Set TabBar background
[self.tabBarController.tabBar insertSubview:[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"TabBar_iOS4_Background"]] atIndex:0];
[self.tabBarController setSelectedIndex:0];
in .h file define like
#interface yourViewcontroller : UIViewController<UITabBarControllerDelegate>
{
//declare your Tabbar controller with #proparty
}
in .m file
//#synthesize here your Tabbar controller
- (void)viewDidLoad
{
self.yourTabbarControler.delegate=self;
[super viewDidLoad];
}
and now put this delegate of UITabbarController
- (void)tabBarController:(UITabBarController *)tabBarController
didSelectViewController:(UIViewController *)viewController
{
NSLog(#"controller class: %#", NSStringFromClass([viewController class]));
NSLog(#"controller title: %#", viewController.title);
if (viewController == tabBarController.moreNavigationController)
{
tabBarController.moreNavigationController.delegate = self;
}
}
You have to "wire up" the delegate, either using code (in ViewDidLoad etc.) or in interface builder.
Look at this answer which explains how to connect a textView delegate (it's pretty much the same): https://stackoverflow.com/a/1785366/764575

Navigation pushview Controller is not working

I am implement the TabBar app for ipad & iphone both. But In IPAD the navigation controller is not working for ipad nib in table cell. The viewdid Load method is not call for ipad.
But it work fine for iphone..
self.navigationController is NULL for ipad only in my class to navigate.
Help me!!
my code is as follow:
//Connet.m:
if(i==1)
{
TwitterController *tc;
if ([self isPad]) {
tc = [[TwitterController alloc] initWithNibName:#"TwitterController_ipad" bundle:nil];
}
else
tc = [[TwitterController alloc] initWithNibName:#"TwitterController" bundle:nil];
[self.navigationController pushViewController:tc animated:YES];
NSLog(#"%#",self.navigationController); /Problem ****NULL*****///////
//TWITTER.m
-(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
//Custom initialization
self.navigationController.navigationBarHidden = YES;
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationController.navigationBarHidden = YES;
NSString *urlAddress = #"https://twitter.com/UJUDGEIT1";
//Create a URL object.
NSURL *url = [NSURL URLWithString:urlAddress];
//URL Request Object
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
//Load the request in the UIWebView.
[webv loadRequest:requestObj];
}
Try the following code:
(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
NSString *nibname=#"";
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad){
nibname=#"TwitterController_ipad";
}
else{
nibname=#"TwitterController";
}
TwitterController *view = [[TwitterController alloc] initWithNibName:nibname bundle:nil];
self.navigationController = [[UINavigationController alloc] initWithRootViewController:view];
self.window.rootViewController = self.navigationController;
[self.window makeKeyAndVisible];
return YES;
}
Write this code in your app delegate class. The viewDidLoad method gets called.
piyush
how you implement your tab bar.
make each controller of tab bar as navigation controller. it may called your view did load method.
following code may help you. it create 2 controller on tab bar. finally at it to root view controller.
-(void)createTabbar{
NSMutableArray *controllers = [[NSMutableArray alloc] init];
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
//Controller 1
PasswordVC *pvc = [[PasswordVC alloc] initWithNibName:#"PasswordVC" bundle:nil];
UINavigationController *passwordNav = [[UINavigationController alloc] initWithRootViewController:pvc];
passwordNav.navigationController.navigationBarHidden=YES;
[controllers addObject:passwordNav];
pvc.title = #"Password";
[pvc release];
[passwordNav release];
//Controller 2
SettingVC *SVC = [[SettingVC alloc] initWithNibName:#"SettingVC" bundle:nil];
UINavigationController *settingNav = [[UINavigationController alloc] initWithRootViewController:SVC];
settingNav.navigationController.navigationBarHidden=YES;
[controllers addObject:settingNav];
SVC.title = #"Settings";
[SVC release];
[settingNav release];
}
else { //ipad
}
_tabBarController = [[[UITabBarController alloc] init] autorelease];
_tabBarController.delegate=self;
_tabBarController.viewControllers = controllers;
_tabBarController.customizableViewControllers = controllers;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1.0];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:_window cache:NO];
_window.rootViewController = _tabBarController;
[UIView commitAnimations];
}

UITabBarController's TabBar is slightly hidden

My UITabBarController's tabBar is slightly off the view, please can you tell me what is wrong with my code:
LoggedInViewController *lvc = [[[LoggedInViewController alloc]
initWithAccount:account] autorelease];
[self presentModalViewController:lvc animated:YES];
- (void)viewDidLoad
{
self.tabController = [[UITabBarController alloc] init];
LoggedInFeedNavigationController *navController;
navController = [[LoggedInFeedNavigationController alloc]
initWithAccount:self.account];
[self.tabController setViewControllers:
[NSArray arrayWithObject:navController]];
[self.view addSubview:self.tabController.view];
[super viewDidLoad];
}
You are adding the tabController view as a subview, but you have not specified where it should be located within its parent view, or how it should be resized when the parent view changes size. Try the following:
- (void)viewDidLoad
{
[super viewDidLoad]; // see note
self.tabController = [[UITabBarController alloc] init];
LoggedInFeedNavigationController *navController;
navController = [[LoggedInFeedNavigationController alloc]
initWithAccount:self.account];
[self.tabController setViewControllers:
[NSArray arrayWithObject:navController]];
UIView *tabView = self.tabController.view;
[self.view addSubview:tabView];
tabView.frame = self.view.bounds;
tabView.autoresizingMask = (UIViewAutoresizingFlexibleWidth |
UIViewAutoresizingFlexibleHeight);
}
Note: you are not required to call [super viewDidLoad], but if you do decide to call it, you should call it at the beginning of your viewDidLoad method, and not at the end.

iPhone: UINavigationController not showing new view

I have the following action that gets called when a button gets clicked. The log message gets printed yet, the view doesn't change. This function is in a UIViewController.
Any ideas what I am doing wrong? Why isn't the new view being displayed with a red background?
- (void)viewDidLoad
{
[levelButton2 addTarget:self action:#selector(clickButton) forControlEvents:UIControlEventTouchUpInside];
//MORE CODE
}
- (void) clickButton
{
NSLog(#"Clicked Button");
UIViewController *myViewController = [[UIViewController alloc] init];
myViewController.title = #"My First View";
myViewController.view.backgroundColor = [UIColor redColor];
//to push the UIView.
[self.navigationController pushViewController:myViewController animated:YES];
//[self.navigationController pushViewController:nextController animated:YES];
}
-(void) clickButton
{
NSLog(#"Clicked Button");
MyFirstController *myViewController = [[MyFirstController alloc] init];
[self.navigationController pushViewController:myViewController animated:YES];
[myViewController release];
}
Then after go into the MyFirstController.m file and
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
if(self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])
{
self.title = #"My First View";
}
return self;
}
I hope it will help you.
You havent used or mention NIB file in that controller..Add NIB file or else make set root view controller of a navigation with a view controller and push the view controller
You have to change your IBAction method a little bit as,
- (void) clickButton
{
NSLog(#"Clicked Button");
MyFirstController *myViewController = [[MyFirstController alloc] initWithNibName:#"MyFirstController" bundle:nil];
myViewController.title = #"My First View";
myViewController.view.backgroundColor = [UIColor redColor];
//to push the UIView.
[self.navigationController pushViewController:myViewController animated:YES];
//[self.navigationController pushViewController:nextController animated:YES];
}

ViewDid appear not working xcode

Hai,
I have created a tab based iRestaura project. I use 5 tab bar items. One of the tabbar name is Customer. When I tap on customer another tabbar is created with 3 viewcontroller programmatically. When I use view didload method tabbarcontroller is created successfully. But when i use view didAppear then tabbar controller is not created tabbar. In both of cases the other three viewcontroller which is created programmatically , there is not view did appear is not working. But all of case I need to use viewDidAppear method .
Plz anyone can help me...
The viewDidAppear method which not working is given bellow .....
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
UITabBarController *tabBarController1=[[UITabBarController alloc] init];
CustomerListViewController *customerListViewController=[[CustomerListViewController alloc] init];
customerListViewController.title=#"Customer List";
UIImage *anImage1 = [UIImage imageNamed:#"customer.png"];
UITabBarItem *theItem1 = [[UITabBarItem alloc] initWithTitle:#"CustomerList" image:anImage1 tag:0];
customerListViewController.tabBarItem = theItem1;
SelectedCustomerViewController *selectedCustomerViewController= [[SelectedCustomerViewController alloc] init];
selectedCustomerViewController.title=#"Selected Customer";
UIImage *anImage3 = [UIImage imageNamed:#"selectedCustomer.png"];
UITabBarItem *theItem = [[UITabBarItem alloc] initWithTitle:#"Selected Customer" image:anImage3 tag:1];
selectedCustomerViewController.tabBarItem = theItem;
InvoiceListViewController *invoiceListViewController=[[InvoiceListViewController alloc] init];
invoiceListViewController.title=#"Invoice List";
UIImage *anImage2 = [UIImage imageNamed:#"invoiceNo.png"];
UITabBarItem *theItem2 = [[UITabBarItem alloc] initWithTitle:#"Invoice List" image:anImage2 tag:2];
invoiceListViewController.tabBarItem = theItem2;
NSMutableArray *controllers=[[NSMutableArray alloc] init];
[controllers addObject:customerListViewController];
// [controllers1 addObject:vc1];
[controllers addObject:selectedCustomerViewController];
[controllers addObject:invoiceListViewController];
///[controllers1 addObject:vc4];
tabBarController1.viewControllers=controllers;
[self.navigationController setNavigationBarHidden:YES animated:YES];
[[self view] addSubview:tabBarController1.view];
for (int t=0; t<[controllers count]; t++)
{
NSLog(#"controller%#",[controllers objectAtIndex:t]);
}
}
Try this:
Firstly use the below function on your Tabbar controller .m file:
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
[viewController viewWillAppear:YES];
}
And in your "viewDidLoad" function of Tabbar controller .m file, use "localNavigationController.delegate = self;" as used in given example below.
UserListing *nearBy = [[UserListing alloc] init];
nearBy.tabBarItem.image = [UIImage imageNamed:#"icoTabNearby.png"];
nearBy.tabBarItem.title = #"Nearby";
localNavigationController = [[UINavigationController alloc] initWithRootViewController:nearBy];
localNavigationController.delegate = self;
Hope it works for you.