0

Alright so here is the deal. I have 5 tabs, 3 of them are just links. So when the user clicks on them, it actually brings them to a website using this:

 - (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item{...}

So the tab bar item pretty much works as a button.

The links work just fine but the problem is that I don't want a view loaded on the click. So initially I added some [UIViewController alloc] for self.viewController (in the tab bar controller). Those view controllers are blank but they allow me to add the icon like so:

UIViewController *vc = [UIViewController alloc];
[vc1.tabBarItem setImage:[[UIImage imageNamed:@"tab-fb"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate]];
vc1.title=@"Facebook";

All of this works fine, but when the user clicks on it, there is a blank black view. I understand this is normal, but how do I get rid of that? I want the user to be able to stay on the current view, but be led to the link.

I tried doing something like this:

- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item{
    selectedIndex=[[tabBar items] indexOfObject:item];

    if(selectedIndex == 1){
        [self setSelectedIndex:0]; //RIGHT HERE
       //...code to go to link...
}
}

but for some reason, the program does not respond to [self setSelectedIndex:0];

Any tips?

Kampai
  • 22,848
  • 21
  • 95
  • 95
NickProvost
  • 367
  • 5
  • 22

1 Answers1

0

This does not work for me either in my tab bar.

However, this approach might work for you. In your app delegate didFinishLaunchingWithOptions:

MyTabBarController.delegate = self;

Then implement this delegate method in your app delegate:

-(BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController 

In the method you can check for the VC like this:

if ([tabBarController.viewControllers indexOfObject:viewController] == indexOfTabToBlock) return false;

This effectively blocks the switch to that tab.

You would have to find some way to communicate this tap to the main view controller that you want to keep the user on so that it can process the link however you want. You could use a notification for example.

Mike Taverne
  • 9,156
  • 2
  • 42
  • 58
  • You don't need to us a notification for this. You can get a pointer to the currently displayed view controller with the selectedViewController property of the tab bar controller from inside the shouldSelectViewController: method. – rdelmar Nov 20 '14 at 07:21