我正在开发一个带有选项卡 View Controller 和两个互补 View 的 iOS 应用程序。 其中之一使用 CoreLocation API 来获取当前速度,因此会消耗大量电池。
我知道:
[locationManager stopUpdatingLocation];
我可以停止速度更新,但我想在用户改变 View 时这样做。
这可能吗?
最佳答案
将此添加到要更新位置的选项卡项的 ViewController
- (void)viewWillAppear:(BOOL)animated {
// Start location manager
[locationManager startUpdatingLocation];
[super viewWillAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated {
// Stop location manager
[locationManager stopUpdatingLocation];
[super viewWillDisappear:animated];
}
当您的 ViewController 出现时位置管理器开始并在消失时停止 .
关于ios - 当 View 改变时做一些事情,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16104067/