简体   繁体   中英

How to stop location updates from another class/view?

I'm currently building an app that allows the user to keep track of the various "trips" they make.

The app consists of two views. The first view consists of 2 buttons that allow the user the start & stop the current "trip".

Pressing the start button segues into the second view which essentially displays the current trip on a map.

The issue I have is stopping the trip. When the user wishes to stop the trip, they should simply hit the stop button in the first view. I would like to then stop the location updates as they are no longer needed.

The issue is that the location manager is inside the second view so I cannot call the stopLocationUpdates method from the first view.

What I've tried: I've setup an NSNotification to tell the second view to stop the location updates. The issue is that this only gets called upon the second view being loaded again. This is useless as upon stopping the trip, the second view will never get loaded (not until another trip is started).

Thanks in advance for any help.

First of all, NSNotification has absolutely no clue about the state of view controllers at all. So if it starts listening only on load, it's probably because you start listening for notifications on load. You can do that in the designated initializer as well, which might solve your problem.

However, maybe you want to re-consider deleting the view controller all together? If it is not used at all, then why keep it in memory in the first place? You could simply release it.

Using NSNotification is a right way to notify components of your application about events. You should look into why that is not working. Alternatively you could have a pointer to both views in your app delegate and call it from anywhere by calling [UIApplication sharedApplication].delegate;

I think a good option would be:

  • create a protocol in the second view with a method to stop location updates and create a property delegate for this protocol.

-implement the protocol in the first view controller.

-assign the delegate of second view controller to the first.

This should be working fine

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM