self.navigationController.tabBarController.selectedIndex not working - ios

I have a UITabBarController which containing 4 different UIViewControllers.
On first tab there is a UINavigationViewController which contains its child UIViewController. without tapping the tab bar, i want to take user on second tab. For this I tried:
self.navigationController.tabBarController.selectedIndex = 1;
But it's not working.
ignore any mistake i am a newbie.
Thank you.

if set delegate
Try this:
self.selectedIndex = 1;
You are the tabBarController :)

Do the following, before trying to change tab:
UITabBarController *tab = self.tabBarController;
if (tab) {
NSLog(#"I have a tab bar");
[self.tabBarController setSelectedIndex: 1];
}
else {
NSLog(#"I don't have one");
}

Try moving your tab switch code to viewDidLoad
I verified that this works on the built in Tabbed Application template project in xcode 4
- (void)viewDidLoad
{
[super viewDidLoad];
self.tabBarController.selectedIndex = 1;
}
I'd also take a look at your AppDelegate class for something along the lines of this
self.tabBarController = [[[UITabBarController alloc] init] autorelease];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:viewController1, viewController2, nil];
self.window.rootViewController = self.tabBarController;
If you don't see these lines anywhere then you'll want to find where else in your code the UITabBarViewController is being initialized and use that object pointer instead.

for calling the tabbarcontroller.selectedindexyou must have to find out the tabbarcontroller from the navigation controller if you dont use push on navigationController just use topViewController property of navigationController to get the instance of your tabBarController and assign it to UITabBarController and us the selectedIndex and this will work fine

just try
self.tabBarController.selectedIndex = 1

we should put follow section in viewDidAppear.
- (void)viewDidAppear:(BOOL)animated
{
NSArray *ary = [self.tabBarController viewControllers];
UIViewController *vc = ary[3];
[self.tabBarController setSelectedIndex:3];
[self.tabBarController setSelectedViewController:vc];
}

If you are presenting a new controller (different from the one that's currently on screen) you should set the index in the completion block of the function to present your view controller. If you set it before it's presented, it may not work.
In my case I've embedded the tab controller inside a nav controller. I present the new nav controller newNavContoller, and once it's presented I set the index in my tab controller tvc:
currentViewController?.presentViewController(newNavController, animated: true, completion: {tvc.selectedIndex = selectedIndex})

Related

pushViewController not working after back

For my code structure:
On AppDelegate, I declared 4 UINavigationController with their own root UIViewController for my UITabBar.
I created one custom UIViewController as template, in where my other UIViewControllers are sub-class.
On my template:
I have my rightBarButtonItem to show current user profile.
// public method added on template
- (void) goToProfile {
NSLog(#"going through...");
ProfileViewController *ctrl = [[ProfileViewController alloc] init];
[self.navigationController pushViewController:ctrl animated:YES];
}
For my leftBarButtonItem:
- (void) goBack {
[self.navigationController popViewControllerAnimated:YES];
}
First click on the rightBarButtonItem, works fine. If I click the leftBarButtonItem to go back then re-click the rightBarButtonItem, it won't work anymore.
In addition, I have a button on one of my UIViewController that is calling the public method goToProfile. And that works fine.
I got help from my co-worker. The approach is something similar to #Vidhyanand900 answer. I hope this will help others in the future.
tabbarSelectedIndex = 1; // profile tab index
ProfileViewController *ctrl = [[ProfileViewController alloc] init];
UINavigationController *navController = [_appDelegate.mainTabBarController.viewControllers objectAtIndex:tabbarSelectedIndex];
[navController pushViewController:ctrl animated:YES];
self.mainTabBarController.selectedIndex = tabbarSelectedIndex;
If you are pushing or popping with UINavigation controllers of UITabBar
Else show UINavigation Controllers inside UITab Bar..i.e; Is Profile View controller is part of UITab bar or not..
Then you need to change the index of tab bar as below
self.tabBarController.selectedIndex=0;//if profile is first tab
ProfileViewController *ctrl = [[ProfileViewController alloc] init];
[self.navigationController pushViewController:ctrl animated:YES];
Hope it helps you...

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;
}

How to navigate to some other View Controller when Navigation Controller back button is pressed?

I have a viewController in which, on the click of back button, I need to go to a specific viewController and call its method.So I created a barButton and added as a BACK button in navigation bar.When its selector is called I can see only see a black screen, nothing else.
Here how I am doing it.
In viewDidLoad
//Back Button in navigation Bar.
UIBarButtonItem *newBackButton = [[UIBarButtonItem alloc] initWithTitle:#"Back" style:UIBarButtonItemStylePlain target:self action:#selector(navigationBackButtonClicked:)];
self.navigationItem.leftBarButtonItem=newBackButton;
The selector below executes and shows a black screen.
-(void)navigationBackButtonClicked:(UIBarButtonItem *)sender {
SharedManager *sharedManagerObject = [SharedManager sharedInstance];
NSString *source = sharedManagerObject.toCityString;
NSString *destination = sharedManagerObject.fromCityString;
NSString *dateStr = sharedManagerObject.dateSelected_String;
BusListViewController *buslist_VC = [self.storyboard instantiateViewControllerWithIdentifier:#"BusListViewController"];
[buslist_VC getBusListForSource:source destination:destination date:dateStr];
[self.navigationController popToViewController:buslist_VC animated:YES];
}
You need to add your buslist_VC to the view hierarchy of your navigation controller before using [popToViewController:animated:]. This is used to display some viewcontrollers already in the stack of your navigation Controller.
Either way what you're asking might be a weird behaviour for your user but you can use :
[self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:self.navigationController.viewControllers.count - 1] animated:NO];
[self.navigationController pushViewController:buslist_VC animated:NO];
You cann't back to a new ViewController, you can push a new ViewController, in your code change this:
[self.navigationController pushViewController:buslist_VC animated:YES];
But keep in mind, you are putting this new viewController inside the other (where you are created a newBackButton), and the default back button come back to this. If you want to come back to the root use this:
[self.navigationController popToRootViewControllerAnimated:YES];
Or, now you can come back to any viewController on the navigation stack, this array:
NSArray *navStacks = self.navigationController.viewControllers;
Using popToViewController: be sure is in the stack.
You should use
NSArray *navStacks = self.navigationController.viewControllers;
select needed view controller and do
[self.navigationController popToViewController:selectedVC animated:YES];

Attempt to present a viewcontroller whose view is not in the window hierarchy

I have a tab bar application. My requirement is I select the 'scan' tab to scan the qr code and navigate/jump immediatley to another 'list' tab. Both 'scan' and 'list' tab are there in the viewControllers array in didFinishLaunchingWithOptions After referring this link, i don't think i need to set the delegate as both the tabs are already present in the hierarchy.
I get this warning in the following line
if(x)
{
listViewCntrl = [[ListViewController alloc] initWithNibName:#"ListViewController" bundle:nil];
listViewCntrl.getFlag = YES;
[self presentViewController:listViewCntrl animated:YES completion:Nil]; // I get the warning here
}
If I comment out the above code and add
[self.tabBarController setSelectedIndex:1];
then I would not be able to get the subView of the listViewController (set flag to show the subview) which i need to display inside the list tab after scanning.
App crashes if I add
[self.tabBarController setSelectedViewController:listViewCntrl];
So how do I display the listView's subview after scanning?
You can try this if you use storyboard:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
ViewController * destViewController = [storyboard instantiateViewControllerWithIdentifier:#"ViewControllerIdentifier"];
[self.navigationController pushViewController:destViewController animated:YES];
You have to set an identifier for your controller in your storyboard.
ListViewController *listController = (ListViewController*)[self.tabController viewControllers][1];
listController.getFlag = 1;
[self.tabBarController setSelectedIndex:1];
The problem is that you're creating an entirely new ListViewController. You say you already have one in the tab controller - you don't need a new one.
You can't use your 3rd option because, again, the two ListViewController's are different objects (they may be of the same class, but they point to a different address).

load page with pushViewController on tabbar

I have UIViewController with tabbar botton, and navigation bar and navigation item,
when I press on my button in navigation Item I want to load a view, I don't know how to load this view on tabbar,
would you please help me,thanks in advance!
-(IBAction) infoPage:(id)sender
{
InfoCtrol *i = [[InfoCtrol alloc] initWithNibName:#"InfoCtrol" bundle:nil];
[self.navigationController pushViewController:i animated:YES];
}
This will load the InfoCtrol controller and set the Touch UpInside Event in xib to the infoPage method
Do you mean you want to hide the tab bar when you push new controller in navigation?
If true. There is a property hidesBottomBarWhenPushed in the UIViewController class.
// Instead of adding ViewController to TabbarController, add NavigationControllers.
// Eg.
UINavigationController *NavController = [[UINavigationController alloc] initWithRootViewController:_viewCtrl1];
[watchListNavController.navigationBar setTintColor:[UIColor blackColor]];
_tabC = [[UITabBarController alloc] init];
_tabCt.viewControllers = [NSArray arrayWithObjects:NavController,_viewCtrl2,_viewCtrl3, nil];
// Now you can use Push and Pop In your _viewCtrl1.
// Do same with all the viewController

Resources