In my app, I have several uiTabBarItems (like search, list of items, account). When I click on account, user is supposed to put in credentials to log in. After successful login, name of tabbaritem is changed and new view controller is pushed (Profile). However, when I click on the profile tabbaritem, login controller(root for that branch of tabbar) is presented. Is there any way to disable this navigation?
Asked
Active
Viewed 133 times
2 Answers
1
Put the navigation controller within the tab bar controller.
that is the view within the tab bar will have a navigation controller.
and you can control the tab bar action using tabBar:didSelectItem:
delegate. hope this helps you.

Prajwal Udupa
- 860
- 5
- 28
-
I've got navigation controller embeded in tabbar controller already. Editing question, and providing screenshot to make it more clear :) – Yanchi Jan 15 '13 at 09:55
-
So if I understand correctly, I can define method tabBar:didSelectItem: in MyProfileViewController and set it explicitly to do nothing? – Yanchi Jan 15 '13 at 09:57
-
yes you will get which tab bar item was clicked and then you may decide on what to do with it. – Prajwal Udupa Jan 15 '13 at 10:09
-
I'll tried to implement this method `-(void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item { if (item == [self.tabBarController.tabBar.items objectAtIndex:2]) { NSLog(@"Click!"); } }` but it's not firing :( I have 3 tabBarItems.. profile is 3rd (0,1,2) – Yanchi Jan 15 '13 at 10:19
-
see if you are getting the alloced object. else it is a pretty good idea to use tags. use if item.tag == 2 then NSLog(@"clicked"); – Prajwal Udupa Jan 15 '13 at 10:30
-
I tried to modify it `-(void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item { NSLog(@"Clicked %@",item); }` to write what has been clicked.. but even this doesn't fire :( In my .h file my interface is `@interface MyProfileViewController : UIViewController
` – Yanchi Jan 15 '13 at 10:37 -
then you may not have properly set the delegate. go through this doc and let us know if problem persists [apple doc of UITabbar](http://developer.apple.com/library/ios/#documentation/uikit/reference/UITabBarController_Class/Reference/Reference.html) – Prajwal Udupa Jan 15 '13 at 10:53
-
You mean http://developer.apple.com/library/ios/#documentation/uikit/reference/UITabBar_Class/Reference/Reference.html right? Because there are two delegates.. UITabBarController and UITabBar. UITabBar has method didSelectItem, so I'm iplementing UITabBarDelegate, not UITabBarController delegate – Yanchi Jan 15 '13 at 10:58
-
@property (retain, nonatomic) IBOutlet UITabBar *tab;in.h -(void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item { if (item == [self.tabBarController.tabBar.items objectAtIndex:1]) NSLog(@"clicked"); }in.m make sure to link it with xib – Prajwal Udupa Jan 15 '13 at 11:02
-
your tab bar is defined in a different controller? it delegates to the controller it is in. You can cal your function from that controller, ie; call your did select item from its original class – Prajwal Udupa Jan 15 '13 at 11:23
-
and for linking it from the xib you just need to right click on the xib and pull it to your .h file. google that thing you will get it – Prajwal Udupa Jan 15 '13 at 11:25
-
Yes but I cannot link outlets from one xib to another (I have tabBar in tabBarController that is root view for my app and is different controller than Profile) – Yanchi Jan 15 '13 at 11:39
-
Ok I'm getting more and more confused, because I don't really know how to set tabBar delegate.. – Yanchi Jan 15 '13 at 11:48
-
see there is a window. and the windows view will have a view controller. and in that view controller there is a tab bar and within the tab bar use navigation controller this will solve your problem. and on how to use tab bar controller delegate make sure u have used this self.tabBarController.delegate = self; [see this for further info](http://stackoverflow.com/questions/9545126/cant-use-tabbar-delegate-methods) – Prajwal Udupa Jan 16 '13 at 05:26
-
Hey Prajwal, sorry for late response, I've been out of office. I updated question with another image. It shows my Interface builder. As you can see, my TabBarController is 2 levels up in hierarchy from my A2 view controller (where I need to, as you said create delegate to my tabbarcontroller). So my problem still is, how to link outlet in A2 with tabbar. T is my root controller (tabbarcontroller), B is just different branch of tabbar – Yanchi Jan 17 '13 at 09:51
-
your question is much more clearer now. forget all i have said and use a flag to check logged in or not.Now you will definitely get which tab is selected in the root view controller using this check with this flag in the root view controller and push the correct view controller from the root view. – Prajwal Udupa Jan 17 '13 at 10:17
-
the place you have given login view controller as the root view controller must be profile view controller – Prajwal Udupa Jan 17 '13 at 10:19
-
Im using NSUserDefaults to save whether user is logged in or not. I however do not understand, how can I present profile view controller as root, when user needs to login first.. Where in code do you decide which controller is root, and how do you change it later? – Yanchi Jan 17 '13 at 10:43
-
let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/22901/discussion-between-prajwal-udupa-and-yanchi) – Prajwal Udupa Jan 17 '13 at 10:53
0
Ok I finally managed (with help of answer that Prajwal provided, you are getting +1 for that ) to solve this.
All I had do was delete my navigation trace at index 0 (so my login controller gets removed from navigation stack) by using this code:
NSMutableArray *navigationArray = [[NSMutableArray alloc] initWithArray: self.navigationController.viewControllers];
// [navigationArray removeAllObjects]; // This is just for remove all view controller from navigation stack.
[navigationArray removeObjectAtIndex: 2]; // You can pass your index here
self.navigationController.viewControllers = navigationArray;
That I found here Removing viewcontrollers from navigation stack