I have seen al most all the other posts about this, but I can’t seem to get it. I am new to iOS and objective-C, and I am following the Big Nerd Ranch Guides for both Objective-C and iOS. One of the challenges I found on their site is to make a simple CoreLocation app, and I am trying to separate out the code into classes so I can reuse it later if I would like to.
I want to get the button in my ViewController.m file to call locationManager in MyLocation.m file, which is the MyLocation class. I have looked into NSNotificaiton, but I could not get that to work correctly either.
I cannot for the life of me figure out what I am missing or doing wrong. I apologize for the simplicity of the question, and I am trying to learn as many basics as possible.
Below are my files:
ViewController.h:
#import <UIKit/UIKit.h>
#import "MyLocation.h"
@interface ViewController : UIViewController
- (IBAction)GetLocation:(id)sender;
@property (weak, nonatomic) IBOutlet UILabel *LocationOutput;
@end
ViewController.m:
#import "ViewController.h"
#import "MyLocation.h"
@interface ViewController ()
@end
@implementation ViewController
@synthesize LocationOutput;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)viewDidUnload
{
[self setLocationOutput:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
- (IBAction)GetLocation:(id)sender {
//WHAT GOES HERE?
}
@end
MyLocation.h:
#import <Foundation/Foundation.h>
#import <CoreLocation/CoreLocation.h>
@class MyLocation;
@protocol MyLocationDelegate <NSObject>;
@end
@interface MyLocation : NSObject <CLLocationManagerDelegate>
@property (nonatomic, strong) CLLocationManager *locationManager;
@end
MyLocation.m:
#import "MyLocation.h"
@implementation MyLocation
@synthesize locationManager = _locationManager;
-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
NSLog(@"Latitude is: %f", newLocation.coordinate.latitude);
}
@end
I know I am not outputting anything yet to the label, but I am doing an NSLog.
Thank you for any help. I really appreciate it.
In order to send messages to an object, you need to have a reference to it. In the two classes you are posting, you have the same issue: you don’t have a reference to them, so you won’t be able to send them messages.
In
ViewController, you need to somehow get a reference to aMyLocationobject. For example, if you changed your code to:This will, on each call to the action, instantiate a new
MyLocationobject, and send it thesomeMethodmessage. In your case, this is a little bit more complex. YourMyLocationclass needs to have an instance ofCLLocationManagerinstantiated. Probably, you need an instance variable for it and do something like this:Then, the
CLLocationManagerinstance you created onMyLocationwill call its delegate method when the location changes. What you should do in that method, is store that location in some instance variable ofMyLocation, so you can access it at a later point.Finally, I would change the
ViewControllerto create theMyLocationinstance only once, at its initialization, or view load:Hope you get the idea and this can get you started.