Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7902879
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T09:37:07+00:00 2026-06-03T09:37:07+00:00

I have seen al most all the other posts about this, but I can’t

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T09:37:09+00:00Added an answer on June 3, 2026 at 9:37 am

    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 a MyLocation object. For example, if you changed your code to:

    - (IBAction)GetLocation:(id)sender {
        MyLocation *myLocation = [[[MyLocation alloc] init] autorelease];
        [myLocation someMethod];
    }
    

    This will, on each call to the action, instantiate a new MyLocation object, and send it the someMethod message. In your case, this is a little bit more complex. Your MyLocation class needs to have an instance of CLLocationManager instantiated. Probably, you need an instance variable for it and do something like this:

    @interface MyLocation {
        CLLocationManager *_locationMananger;
    }
    @end
    
    @implementation MyLocation
    
    - (id)init {
       self = [super init];
       if (self) {
           _locationManager = [[CLLocationManager alloc] init];
           _locationManager.delegate = self;
       }
       return self;
    }
    
    - (void)dealloc {
       [_locationManager release];
    
       [super dealloc];
    }
    
    @end
    

    Then, the CLLocationManager instance you created on MyLocation will call its delegate method when the location changes. What you should do in that method, is store that location in some instance variable of MyLocation, so you can access it at a later point.

    Finally, I would change the ViewController to create the MyLocation instance only once, at its initialization, or view load:

    @interface ViewController {
        MyLocation *_myLocation;
    }
    @end
    
    @implementation ViewController
    
    - (id)initWithNibName:(NSString *)nibName bundle:(NSBundle *)bundle {
        self = [super initWithNibName:nibName bundle:bundle];
        if (self) {
            _myLocation = [[MyLocation alloc] init];
        }
        return self;
    }
    
    - (void)dealloc {
       [_myLocation release];
       [super dealloc];
    }
    
    - (IBAction)GetLocation:(id)sender {
       // Grab the location from _myLocation using a property or method defined
    }
    
    @end
    

    Hope you get the idea and this can get you started.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have seen this talked about but never answered. Maybe it has and I'm
I am new to DBMS_PROFILER. All the examples I have seen use a simple
I have read the other posts about the notorious _imaging C module error when
In most samples I have seen on the web, DI in MVC Controllers is
I have seen many posting this issue in SO. I have gone through those
I have seen this question and its answers and they clear up some of
I've seen various posts about how to implement the deletion of dependent rows from
Each time I have seen the catch all statement: try { // some code
Not sure exactly how to explain this but Imaging you have a table with
Since the last week in January we've seen most of our iOS apps, and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.