PeppeLaKappa
PeppeLaKappa

Reputation: 139

Do something when view changes

I'm developing an iOS application with a Tab View Controller and two complementary views. One of these uses CoreLocation API to get current speed and because of this, it uses a lot of battery.

I know that with:

[locationManager stopUpdatingLocation];

I can stop the speed updating, but I want to do this when the user change view.

Is this possible?

Upvotes: 1

Views: 141

Answers (3)

Daij-Djan
Daij-Djan

Reputation: 50089

you need to implement the delegate of the UITabbarController. Then you get:

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

and you can return YES and stop the locationManager


- (void)applicationDidFinishLaunching:(UIApplication *)application
{
    tabBarController.delegate = self; //you need an iboutlet to get the tabbar
    [window addSubview:tabBarController.view];
}

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController 
{
    if([viewController respondsToSelector:@selector(locationManager)])
        [[viewController locationManager] stopUpdatingLocation]; //your viewController needs to expose the locationManager as a property
    return YES;
}

Upvotes: 0

ios_av
ios_av

Reputation: 324

Just add this [locationManager stopUpdatingLocation];, when the action to change the page performed.

Upvotes: 0

Navnath Godse
Navnath Godse

Reputation: 2225

Add this to your ViewController of tab item where you want location update

- (void)viewWillAppear:(BOOL)animated {    

    // Start location manager
    [locationManager startUpdatingLocation];

    [super viewWillAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated {

    // Stop location manager
    [locationManager stopUpdatingLocation];

    [super viewWillDisappear:animated];
}

When your ViewController appear location manager get's start and stop when disappear .

Upvotes: 2

Related Questions