I have a UITabBarController setup with 2 UINavigationControllers.
One UINavigationController has One UIViewController, the other UINavigationController has Two UIViewControllers. If you then navigate to the second UIViewController and click the Tab that is already selected it bring you to the root of the UINavigationController (This would be the first UIViewController).
Is there a way to stop this from happening? I do not want the user to be able to click an already selected Tab to go to the root of the Navigation Controller.
To do this you need to implement a function in your app delegate to pick up the tabbar delegate calls.
In your app delegate.m file, in the didfinishlaunching method, add this line
[tabBarController setDelegate:self];
then implement this method (also in your app delegate):
- (BOOL)tabBarController:(UITabBarController *)theTabBarController shouldSelectViewController:(UIViewController *)viewController
{
return (theTabBarController.selectedViewController != viewController);
}
This gets called as part of the tab delegate protocol and will stop the selection of a tab if its already the selected one.
Hope that helps.
Related
I'm building an app that combines tabbar and navigation bar. In one of tabs, I have a UIButton and by touching it, I push a Tableview into stack of navigationcontroller. Then, without returning to the root view controller manually by pressing Back buttons I change to another tab from tabbar and when I come back to the tab with the tableview inside, I get bad access error.
I've already tried popping the tableview from navigation controllers stack, or not releasing the tableview but I could't make it.
Thanks...
Assuming you've created your tabBar and Navigation Controllers in the AppDelegate, you can code your app to return each tab to the root view when it is selected.
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
if (viewController == firstViewNavigationController) {
[firstViewNavigationController popToRootViewControllerAnimated:NO];
} else if (viewController == secondViewNavigationController) {
[secondViewNavigationController popToRootViewControllerAnimated:NO];
}
}
I am using this and it is working fine in my app. I am releasing my Navigation Controllers in the dealloc method. I have also made my AppDelegate conform to the UITabBarDelegate protocol.
hii, there
I am making a tabbar application.In this application on the first tab I am using navigation so when I navigate to other view and instantly than I goes to other tab. And again when I am going to first tab it show me navigate view I want to show first tab view.
Somebody can tell me how can I manage these things..
Are you asking how to switch from one tab to another in code? If so it looks like this:
YourAppDelegate *appDelegate = (YourAppDelegate *)[[UIApplication sharedApplication] delegate];
[appDelegate tabcontroller].selectedIndex = [yourIntegerIndexHere];
Where it says "YourAppDelegate," obviously use the name of your actual app delegate.
Where it says "yourIntegerIndexHere," that's an integer with the index of the tab you want to switch to. The left-most tab is 0.
Thats the main purpose of the tabbased application that all your views are saved till they are navigated.
If you want the initial view to be there when you tap on the tabbar then on every tabbars view you have to code for poping your view in the first tab to the rootview controller for that tab.
hAPPY iCODING...
As I understand, you have a NavigationController on your first Tab of your TabBarController and, when you click on the first Tab, you want your NavigationController to go back to its root view controller.
First of all, be aware that this is not the default behavior of a TabBarController and it might be a bit annoying to your users. The user can go back to the rootview of a NavigationController inside a TabBarController by tapping the tab a second time.
Knowing this, if you still want to change the default behaviour of your TabBarController, here is what you can do :
Set your AppDelegate class to be the Delegate of your UITabBarController. It has to implement the UITabBarControllerDelegate protocol, and you have to write something like this :
[myUITabBarController setDelegate:self];
inside your application didFinishLaunchingWithOptions: method.
Then, implement this method inside your AppDelegate.
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
if ([tabBarController selectedIndex] == kMyNavigationControllerIndex) {
[(UINavigationController *)[tabBarController selectedViewController] popToRootViewControllerAnimated:NO];
}
}
Where kMyNavigationControllerIndex is a constant value containing the index of the NavigationViewController you want to change (i.e. 0 if it is the first tab).
Hope this helps.
I have a tabbar app. each tab has uinavigationcontroller. but all navigationcontroller's Shows Navigation Bar value is NO.
i control the which navgation control is active in
-(void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
event in AppDelegate. Then i the user taps a row in a table in a tab i call the same method (openDetail)in appdelegate. i want to push DetailViewController full screen, not in tabs controller. i tried some ways but never worked. so i push it as a modalview.
[currentNavController presentModalViewController:detailVC animated:YES];
how can it come/go from right side.
i just want it to work like normal rootviewcontroller. but new controller should come over tabbarcontroller like USA Today app.
present the view controller like this
[tabBarController presentModalViewController:detailVC animated:YES];
I have four UINavigationControllers assigned each to a tab in a UITabBarController. Each UINavigationController manages a UIViewController, which may itself branch out into other UIViewControllers below it hierarchally.
My question is, in a case in which a user, under one tab, has navigated to a UIViewController that hierarchally BELOW the main UIViewController managed by the UINavigationController, and then the user pushes a different tab, and then goes back to the original tab, HOW can I make it so that the user is presented with the main UIViewController managed by the UINavigation controller? and not the page where he left off?
UITabBarController is set up in IB
Implement the UITabBarControllerDelegate protocol and pop to the root controller whenever your delegate is notified that the user has selected a different tab.
Something like:
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
[viewController.navigationController popToRootViewControllerAnimated:YES];
}
The code above should pop to the root controller of any navigation controller that is the main view controller of the newly selected tab. You could try implementing the protocol in your application delegate. And do not forget to actually assign the app delegate as the tab bar controller's delegate.
didn't work for me originally, then later on I noticed that all my viecontrollers for different tabs are actually UINavigationControllers
and thus I modified the above code a little bit as follows and it worked:
[(UINavigationController *)viewController popToRootViewControllerAnimated:NO];
I have the next question:
In my project I have the next:
UItabbarController
....Some UINAvigationControllers....
*(1) UINavigationController
UIViewController (UItableView) - When select one row it goes to...(by push)
UIViewController (UItableView)
My problem is when i click in the tab bar item, I see the viewController view like last time that i saw this, and no reload to the *(1) first view another time.
Where I need to write sth for each time that i click in a tab bar item i reload the first view of this tab bar item.
Thanks!
If I understand your question correctly, you are trying to return the navigation controller to the root element when it's tab bar item is selected.
To do this, set some object (e.g. your application delegate, but it can be some other object instead) to be the delegate for your UITabBarController. (If you use the application delegate, it will be the delegate for more than one thing, which is fine.) Then, implement the tabBarController:didSelectViewController: method. In that method, tell the selected view controller (which should be a NavigationController) to return to the root view controller.
Something like this. Add this implementation to your AppDelegate.m class:
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
[viewController popToRootViewControllerAnimated:NO];
}
In your .xib file, set the delegate for the TabBarController to the the AppDelegate. (If you are programmatically creating the tabBar, you'll have to do it programmatically there.)
As you suspected, trying to do this in viewWillAppear method, or anything other method, of the view controller that live in the navigation controller is not the correct approach. It is a method to perform on the navigation controller, and detected by the delegate of the tab bar.
Try putting your callback code to reload the view in the viewWillAppear or viewDidAppear methods. These are both called every time a view controller displays it's view on the screen.
Also feel free to copy and paste your actual code, it usually makes things easier on our end :)