-2

I'm pretty new to programming, I'm working on an app with a UITabBarController

I want to open another app when one of the Tab Bar Items is selected. I put the code in the viewDidLoad, and when I start the app and click on the Tab Bar item for the first time it works great, but after that when I click on it again without restarting the app nothing happens.

I have tried using tabBarController didSelectViewController, and tabBar didSelectItem to try and fix this. But can not seem to get them to work. Is this a lost cause, or am I just missing something, or not putting them in the right place?

Code:

-(void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController { 
    if (tabBarController.selectedIndex == 2) { 
        [[UIApplication sharedApplication] openURL: [NSURL URLWithString:@"mswxmedia://"]]; 
    } 
}
G.L.P
  • 7,119
  • 5
  • 25
  • 41
Josh Kern
  • 3
  • 2

1 Answers1

0

It's hard to give a good answer without seeing your code, but I think that if you move the code that opens the other app from viewDidLoad to viewWillAppear or viewDidAppear, it might work.

viewDidLoad is called once when the view is loaded for the first time. Then, the view stays in memory, and when you tap on the tab bar item again, it's not loaded again, because it's already there. It sounds like you want to do that every time the view appears, so viewWillAppear or viewDidAppear seem like better candidates for your needs.

Also, probably a better solution would be to implement the UITabBarControllerDelegate and implement the tabBarController:shouldSelectViewController: method to check which viewController will be selected and open the other app from there.

  • I have -(void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController { if (tabBarController.selectedIndex == 2) { [[UIApplication sharedApplication] openURL: [NSURL URLWithString:@"mswxmedia://"]]; } } as my code, all the answers I saw on how to do this when like back in 2010, so not sure if the way to do it has changed since then. – Josh Kern Jul 01 '15 at 08:44