views:

362

answers:

2

Hi all,

I'm currently writing a app on the iPhone and was just wondering if anyone knew a way to call a function but only when a tab is selected.

I have a separate view controller for the tab in question and I want to call a function which updates the GPS position when the tab is selected.

If anyone could help that would be great :)

Thanks - James

+2  A: 

If you implement the UITabBarControllerDelegate protocol somewhere, you can handle when a tab is pressed if you set the class that implements it as the tab controller's delegate. That way you can call the correct method when you need it. The reference is here; the method you probably need is tabBarController:didSelectViewController:

Kevlar
I've just seen this function commented out in my appdelegate but it doesn't seem to ever get called. :/ Thanks for helping though!
ing0
Are you setting the tabbarcontroller's delegate to the class who implements that method? that may be why it isn't called.
Kevlar
+2  A: 

Just incase anyone else has this issue I instead called the 'viewDidAppear' method in the view controller which is linked to the tab.

- (void)viewDidAppear:(BOOL)animated {
    // do stuff here    
}

This soultion was more ideal for me as I was only wanting to check for one tab anyway!

ing0