6

I know this question has been asked a few times, but I am still stuck on the case when I have my UITabBarController in my AppDelegate class and viewControllers are set there only as

self.tabBarController.viewControllers = 
     @[aboutUsNavController,myProfileNavController,
      projectsListNavController, feedsNavController,homeViewController];

Now what I want is to perform a task at the tap of TabBar item feedsNavController not in viewWillAppear (because it is pushing a detailView via navigationController ).

I have set delegate in the same class to perform a UITabBarController delegate method:

 AppDelegate *appDelegate = 
         (AppDelegate *)[[UIApplication sharedApplication] delegate];
appDelegate.tabBarController.delegate = self;

But -(void) tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController only works when I switch the TabBar Item, but not at the first time?

Is there any way I can call UITabBarDelegate instead:

-(void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item

in order to get the reference when tabBar item is pressed?

Or any other approach would be appreciated.

Mani
  • 17,549
  • 13
  • 79
  • 100
Samapple007
  • 355
  • 4
  • 15

3 Answers3

11

Figured out the way to do it:

Actually UITabBarControllerDelegate method only gets called once that particular tab it loaded.

Therefore, I performed task [self getFeedsFromServer]; in viewDidLoad (for first time), and then again in

-(void) tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
    NSLog(@"Selected INDEX OF TAB-BAR ==> %i", tabBarController.selectedIndex);

    if (tabBarController.selectedIndex == 3) {
      [self getFeedsFromServer];
    }
}

Works as I wanted!

Samapple007
  • 355
  • 4
  • 15
1

If you want to perform task 1 time then ViewDidLoad is best for it. ViewWillAppear calls every time your ViewController get focused but ViewDidLoad is called just when you push that Controller on stack.

Kashif Ilyas
  • 136
  • 1
  • 2
  • 7
0

What do you mean by 'because it is pushing a detailView via navigationController' ? Your question is not so much clear.

But maybe this thing me help you by my understanding:

- (void)tabBarController:(UITabBarController *)theTabBarController didSelectViewController:(UIViewController *)viewController    
 {

        NSUInteger indexOfTab = [theTabBarController.viewControllers indexOfObject:viewController];
        NSLog(@"Tab index = %u (%u)", indexOfTab);

    }
Avijit Nagare
  • 8,482
  • 7
  • 39
  • 68
Kashif Ilyas
  • 136
  • 1
  • 2
  • 7
  • I have tried and explained that I have NavigationControllers set on `tabBarController.viewControllers`, so there is a tableView which pushes that particular view, i.e., I cannot perform my task on `viewWillAppear` because everytime it I `popToViewController` `viewWillAppear` will get called – Samapple007 Dec 12 '13 at 07:42