Reputation: 842
I am trying to find the solution to what seems like an obvious problem but have had no luck.
I am calling a NSObject
from my ViewController using the method below which initiates the CLLocationManager
and returns the the current location as it should.
locationObject *location = [[locationObject alloc] init];
[location updateLocation];
What I cannot do however is pass that data back to the view controller without it causing problems. I have tried using...
ViewController *controller = = [[ViewController alloc] init];
[controller updateContent:lat longitude:lng];
This prints out everything via NSLog which is in the updateContent
method in the ViewController but it doesn't update any of the labels or call any of the methods.
I am trying to simply update a UILabel
but nothing happens. I am sure this is something to do with the fact I cam calling just one method and not the whole view controller.
I think I am calling the ViewController incorrectly, I think I should be calling it as a parent instead but for the life of me I can't figure out how to do it!
Thanks for your help in advance.
Upvotes: 0
Views: 397
Reputation: 9835
So you have a UIViewController - let's call it 'A' that you've created and it has a label as a subview. Then you also have an object that I'm guessing is your CLLocationManager Delegate. What you're doing is creating a completely new UIViewController - 'B', and tell it to update location, but you aren't referencing the original 'A' view controller, which is why the log statements appear to log the correct information but nothing changes in 'A'.
There are a few ways to go about this, but I think the easiest to grasp is to use NSNotificationCenter to broadcast a message to your app saying "Hey, I've got a new location! Do something with it!'. The second step is allow outside objects to reference this location, perhaps by adding a property to your location object. I would do the following and see if it works:
In your location object's .h file add something like this:
@property (nonatomic, retain) CLLocation *currentLocation;
When your location object receives an updated location from the CLLocationManager, I would do this:
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
// get the most recent object from the CLLocationManager and set it as your currentLocation
self.currentLocation = [locations lastObject];
// broadcast to the application that you have a new location
[[NSNotificationCenter defaultCenter] @"GotNewLocation" object:nil userInfo:nil];
}
Then, inside your view controller, add this somewhere like your init or viewDidLoad method:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleNewLocation) name:@"GotNewLocation" object:nil];
Then, handle what you need to do inside your handleNewLocation function:
- (void)handleNewLocation {
// get the current location from the location object and use its coordinate to do what you need
CLLocation *currentLocation = yourLocationObject.currentLocation;
[self updateContent:currentLocation.coordinate.latitude longitude:currentLocation.coordinate.longitude];
}
Upvotes: 1