didSelectViewController does not work - ios

I added a tab bar controller programatically, and I want to switch view and hide tab bar controller with the method:
-(void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
But it is not calling. I add delegate <UITabBarControllerDelegate> and [myTabBarController setDelegate:self]; in viewDidLoad.
What can I do?
Thanks for your reply. Please help..

Try this in your app delegate's didFinishLaunchingWithOptions method:
UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController;
tabBarController.delegate = self;

Related

How to get the RootViewController from a UITabBarItem

Im not even sure if the title is correct but i do not understand how else to explain it so here it goes. I will show you in steps what i do so you will understand.
First off , i am using UITabBarController where i got three different items.
1) The app starts and it shows me the first Item which is a UITableView
2) I click on a cell and it pushes me to a detailed view of the cell.
3) I navigate from there to another TabBarItem
4) I navigate back to the first TabBarItem and it still shows me the view from step 3). I DONT want that i want it to show me the first view from step 1)
How can i achieve this?
You can implement UITabBarController delegate method:
- (void)tabBarController:(UITabBarController *)tabBarController
didSelectViewController:(UIViewController *)viewController
Which will let you know when a tab is selected.
You can then access the UIViewControllers of your UITabBar using if you don't have a reference to the UIViewControllers:
#property(nonatomic, copy) NSArray *viewControllers
From that grab the refence to the appropriate UIViewController (or UITableViewController) that you want to reset and call:
- (NSArray *)popToRootViewControllerAnimated:(BOOL)animated
On it's UINavigationController. Or create a public method for that call as call popToRootViewController that way.
EDIT:
Your AppDelegate should look like this:
#interface AppDelegate () <UITabBarControllerDelegate>
#end
#implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController;
tabBarController.delegate = self;
return YES;
}
- (void)tabBarController:(UITabBarController *)tabBarController
didSelectViewController:(UIViewController *)viewController {
NSLog(#"was selecated");
}
Assuming you've set up your tab bar controller in the your AppDelegate, you want to make it your <UITabBarControllerDelegate>
And in your AppDelegate.m you want to implement this method like this:
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
if ([viewController isKindOfClass:[UINavigationController class]]) {
[(UINavigationController*)viewController popToRootViewControllerAnimated:YES];
}
}

In didFinishLaunchingWithOptions method, I can not set index of tabbar

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
...
if (condition) {
UITabBarController *tabbar = (UITabBarController *)self.window.rootViewController;
[tabbar setSelectedIndex:4];
}
...
}
In didFinishLaunchingWithOptions method, I can not set selected index of tabbar.
UITabbarController is initial viewController in my project.
I want to set another tabbar index for special condition, when users launch my app.
I have tested ios7, ios8.
Is there any other way?
I think that the problem in your code, is that there's no identified UITabBarController... If you're using storyboard it's easy to make this connection -
UIStoryboard *storyboard = self.window.rootViewController.storyboard;
UITabBarController *tabBarViewController = (UITabBarController *)[storyboard instantiateViewControllerWithIdentifier:#"tabbar"]; //Set this identifier in the storyboard
[tabBarViewController setSelectedIndex:1];

How to pop to root view controller on custom tab bar button clicked iOS

I am showing custom tab bar using tab bar controller.
And Creating Separate navigationController for view controllers.
First *firstViewController = [[First alloc]init];
UINavigationController *firstNavController = [[UINavigationController alloc]initWithRootViewController:firstViewController];
Second *secondViewController = [[Second alloc]init];
UINavigationController *secondNavController = [[UINavigationController alloc]initWithRootViewController:secondViewController];
Third *thirdViewController = [[Third alloc]init];
UINavigationController *thirdNavController = [[UINavigationController alloc]initWithRootViewController:thirdViewController];
tabBar.viewControllers = [[NSArray alloc] initWithObjects:firstNavController, secondNavController, thirdNavController, nil];
tabBar.delegate=self;
tabBar.selectedIndex=0;
but when i am trying to pop to root on tab click, Only 3rd navigation controller is accessible.
So its working for only 3rd tab, First and second is not working.
If you're loading a view controller on top of a tabBarController this is how you would dismiss your loaded view controller.
[self.presentingViewController dismissViewControllerAnimated:self completion:nil];
UINavigationController * navController = (UINavigationController *) [[[tabbarcontroller viewControllers] objectAtIndex: tabbarcontroller.selectedIndex] rootViewController];
If you want all those viewcontrollers to be popped, you may find this delegate useful.
- (BOOL)tabBarController:(UITabBarController *)tabBarController
shouldSelectViewController:(UIViewController *)viewController {
//iterate though tabbar-viewcontrollers to pop all of them
//return NO, if you want this to be handled like an action
return NO;
//return YES, if you want this to be handled like a normal selection
return YES;
}
Hope it helps
It will pop to rootViewController when tap on the tab.
-(BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController{
[(UINavigationController*)viewController popToRootViewControllerAnimated:YES];
return YES;
}

click navigation tabbaritem popToRootViewController

first I Install TabBarController directly as a window’s root view controller,and hava a NavigationController in viewControllers.
when the app run, I push some new viewcontroller onto the navigation stack
than I tap navigation tabbaritem, the navigationController
poptoRootViewController
How can I crash the step 3 event or stop it pop to root?
My solution to stop auto pop to root viewcontroller:
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
UITabBarController *tbc = [[UITabBarController alloc]init];
tbc.viewControllers = [NSArray arrayWithObjects:rvc,nil];
tbc.delegate = self;}
-(BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController{
if([tabBarController selectedViewController] == viewController)
return NO;
return YES;}
if you have an anther solution,wellcome write down.
try this code in tab bar app
[self.tabBarController.navigationController popToRootViewControllerAnimated:YES];
You implement didSelectViewController method in new viewController(push to new viewController)
Import UITabBarControllerDelegate,
- (void)viewDidLoad
{
[super viewDidLoad];
self.tabBarController.delegate = self;
}
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
[self.tabBarController.navigationController popToRootViewControllerAnimated:YES];
}

TabBarController in a NavigationConroller

I have a UITabBarController within a UINavigationController, I know the iOS documentation for the UINavigationController says the following:
rootViewController
The view controller that resides at the bottom of the navigation stack. This object cannot be an instance of the UITabBarController class.
So does this mean that if I have UIViewControllers already on the navigation stack, it's ok then to push a UITabBarController, once it's not the root item?
I have this at the moment and all seems ok except when I pop the UITabBarController, the dealloc or viewDidUnload isn't called in any of the TabBarItems ViewController, do I need to do something similiar for getting the viewWillAppear to work?
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
[viewController viewWillAppear:NO];
}
Thanks
I am not sure that having a UITabBarController within a UINavigationController is going to work.
I usually do this the other way round
companyNavController = [[[UINavigationController alloc] initWithRootViewController:companyViewController] autorelease];
companyNavController.tabBarItem = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemSearch tag:1];
[tabBarController setViewControllers:[NSArray arrayWithObjects:phoneBookNavController, companyNavController, faveNavController, settingsNavController, nil]];
If you'd like to hide the TabBar part of your App, you could always try hidesBottomBarWhenPushed to manage this.
HTH

Resources