When I receive a Push Notification, I want to jump to the middle tab on the tabbar and reset the tab (it has multiple table views that the user may have navigated into). I am able to jump to the tab using tabBarController.selectedIndex = 1; ... but how do I reset to the first view?
Implement UITabBarControllerDelegate on a controller class (perhaps your app delegate) and implement tabBarController:didSelectViewController: to pop all pushed view controllers off the navigation stack:
- (void) tabBarController:(UITabBarController *) tabBarController didSelectViewController:(UIViewController *) viewController {
if([viewController isKindOfClass:[UINavigationController class]])
{
[(UINavigationController *)viewController popToRootViewControllerAnimated:YES];
}
}
Related
I am working on an application where I have a UINavigationController embedded inside a UITabBarController. The UINavigationController has a UITableView that transitions to a DetailController when a cell is tapped.
My question is: I select a cell on [TAB1] and transition to the DetailController. If I select [TAB2] and then go back to [TAB1] its still on the detail controller. Is there anyway when [TAB2] is selected that I can unwind/dismiss the DetailController on [TAB1] (i.e. so its showing the table view cells again).
My other line of thinking is that doing this would not leave the UI in the state the user left it in, i.e. after viewing the DetailController and pressing [TAB2] returning to [TAB1] would present the user with the cells in the UITableView. It feels better for the application to revert the DetailController if you leave the TAB, which is why I am asking...
NB: I present the *DetailController* via a push segue from theUITableViewCell.
You can implement UITabBarControlleDelegate and go back to root view of UINavigationController using popToRootViewControllerAnimated: when tab is changed.
Code example:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController;
tabBarController.delegate = self;
return YES;
}
- (BOOL)tabBarController:(UITabBarController *)tabBarController
shouldSelectViewController:(UIViewController *)viewController
{
UIViewController *currentController = tabBarController.selectedViewController;
if ([currentController isKindOfClass:[UINavigationController class]])
[(UINavigationController *)currentController popToRootViewControllerAnimated:NO];
return YES;
}
I want navigate to Rootview of 1st Tabbar item by clicking a button on fourth tabbar using this code just changes the Tabbar selection
code snippets
[self.parentViewController.tabBarController setSelectedIndex:0];
Previous action to be appear in the home view controller .
i need Direct navigation of Home view controller in main page
How to resolve in this issue?
Thanks in advance
First get your UINavigationController of the first tabbar item.
UINavigationController *navController = [self.tabBarController.viewControllers objectAtIndex:0];
And then navigate to root view controller.
navController popToRootViewControllerAnimated:NO];
That's all. :)
You have to pop the selected tab bar navigation stack to root.. You can achieve this by several method one is as below..
In your AppDelegate implement the tabbarcontroller delegate function, make sure you have set the tabbarcontroller delegate to AppDelegate..
-(void) tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
//Check the selected index to 0
if ([viewController isKindOfClass:[UINavigationController class]] && tabBarController.selectedIndex == 0) {
[(UINavigationController *)viewController popToRootViewControllerAnimated:NO];
}
}
I am assigning my view controller to my tab right after I create it. Is it possible to select the view that will show after the tab is clicked?
For eg
//user clicks tab 1
if(hasMessages)
//show view A
else
//show view B
Yes, it is possible. You need to set a delegate for your tab controller:
UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController;
tabBarController.delegate = self; // or whatever suitable class you have
This delegate needs to conform to the UITabBarControllerDelegate protocol.
In your delegate, implement tabBarController:didSelectViewController: and inside it, find out which view you want to present. Assuming your tab's root view controller is a navigation controller, then the delegate method implementation would be something like this:
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
/* logic goes here */
[viewController pushViewController:someNewVC animated:YES];
}
I could use some help debugging this issue if anyone has had experience with it. I have a UITabBarController as my root view controller. The first view controllers of each tab are UINavigationController.
Tab 1:
A view with a map is the first viewController.
When you push on the callout, this code is run:
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control {
[self performSegueWithIdentifier:#"ShowLocationDetails" sender:self];
}
In the storyboard, I have this wired up as Push.
On the first viewController, because I have a map and don't want to have a navigation bar show, I do this:
FirstViewController <UINavigationControllerDelegate>
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
[self.navigationController setNavigationBarHidden:YES animated:NO];
}
- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
[self.navigationController setNavigationBarHidden:YES animated:NO];
}
On the second view controller that I push onto the stack, I do want the navigation bar, so I have this code:
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
if ([viewController isKindOfClass:[FirstViewController class]]) {
[self.navigationController setNavigationBarHidden:YES animated:YES];
}
else {
[self.navigationController setNavigationBarHidden:NO animated:YES];
}
}
Tab 2:
Just a tablview for right now.
I can get my app to crash if I do these simple steps:
Press the callout to push the LocationDetails.
Press back to go back to First View Controller
Press tab two to go to a TableViewController
Press tab one to go back to the map and this crashes it with an EXC_BAD_ACCESS. I tried running Instruments, but I didn't know what it meant:
If I start the app, and just switch between the tabs all day without pushing a new controller onto the stack on tab 1, it does NOT crash.
It also does not crash if I remove the navigationController willShowViewController delegate methods. But of course then I have the navigation bar which is not what I want.
Any thoughts? Thanks!
EDITED
So I tried what Nuzhat said and I get the same problem. I tried both
self.navigationController.navigationBar.hidden = YES;
and
self.navigationController.navigationBardHidden = YES;
and it isn't the solution I"m looking for. What happens is when I push to the next view controller the navigationBar is not there. So in that viewController's viewDidLoad, I set
self.navigationController.navigationBar.hidden = NO;
Then I pop back to FirstViewController, then I switch tabs, still crashes. There seems to be a problem with hiding the navigation bar, and then showing it again.
When i press certain Tab it's not going to its rootviewcontroller,
When user changes a tab, for the selected tab I want to push it to its top level controller.
I have implement this method but not works,
-(void)tabBarController:(UITabBarController *) tabBarController didSelectViewController : (UIViewController *)viewController
{
[viewController.navigationController popToRootViewControllerAnimated:NO];
}
What's wrong with this?
How can i do that?
Assuming the method gets called (if not you should set the UITabBarController delegate), you are probably receiving the UINavigationController (which is a subclass of UIViewController) as viewController, you can check by logging it:
-(void)tabBarController:(UITabBarController *) tabBarController didSelectViewController : (UIViewController *)viewController
{
NSLog(#"didSelect %#", viewController);
[viewController.navigationController popToRootViewControllerAnimated:NO];
}
if that is the case, viewController.navigationController will probably be nil, you should be doing:
-(void)tabBarController:(UITabBarController *) tabBarController didSelectViewController : (UIViewController *)viewController
{
if ([viewController isKindOfClass:[UINavigationController class]])
[(UINavigationController*)viewController popToRootViewControllerAnimated:NO];
}