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 4101564
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T20:42:49+00:00 2026-05-20T20:42:49+00:00

I want to have one object that is initialized in the delegate and I

  • 0

I want to have one object that is initialized in the delegate and I want to be able to use this object anywhere across view controllers (doesn’t depend on what view I am currently at). I am guessing the solution to this would be to have a singleton class, so far I have the following:

@interface LocationManager : NSObject <CLLocationManagerDelegate>{
    NSDate *enter;
    NSDate *exit;
    CLLocationManager * manager;

}

@property (nonatomic, retain) NSDate * enter;
@property (nonatomic, retain) NSDate * exit;

- (BOOL)registerRegionWithLatitude:(double)latitude andLongitude:(double)longitude;
+ (LocationManager *)instance;

@end


#import "LocationManager.h"

@implementation LocationManager
@synthesize enter;
@synthesize exit;

#pragma mark - CLLocationManager delegate
static LocationManager *gInstance = NULL;


+ (LocationManager *)instance
{
    @synchronized(self)
    {
        if (gInstance == NULL)
            gInstance = [[self alloc] init];
    }
    return(gInstance);
}

@end

Is this correct? So all I need to do to access this is just to call instance? Inside LocationManager I also want to have only one CLLocationManager, called manager.. however, where do I initialize it so I only have one? Can I do the following? Most other singleton examples doesn’t have any variables in the class, so that’s where I got confused

+ (LocationManager *)sharedLocationManager
{
    @synchronized(self)
    {
        if (lm == NULL){
            lm = [[self alloc] init];
            lm.manager = [[CLLocationManager alloc] init];
            lm.manager.delegate = lm;
        }
    }
    return(lm);
}
  • 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-05-20T20:42:50+00:00Added an answer on May 20, 2026 at 8:42 pm

    Basically — yes.
    Just a couple of small things:
    static LocationManager *gInstance = NULL;
    instead of NULL, you should use nil, it’s a convention in Objective-C.

    You should also overwrite alloc, new, copyWithZone:, and mutableCopyWithZone:. From Buck/Yacktman: “Cocoa Design Patterns”, p. 153:

    + (id)hiddenAlloc  
    {
      return [super alloc];
    }
    
    + (id)new
    {
      return [self alloc];
    }
    
    + (id)allocWithZone:(NSZone *)zone
    {
      return [[self sharedInstance] retain];
    }
    
    - (id)copyWithZone:(NSZone *)zone
    {
      [self retain];
      return self;
    }
    
    - (id)mutableCopyWithZone:(NSZone *)zone
    {
      return [self copyWithZone:zone];
    }
    

    This way, your singleton object cannot be copied. You need to call hiddenAlloc from your instance method (by the way, the method to access a Singleton object is often called sharedInstance in Objective-C).

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

Sidebar

Related Questions

I have one very general object that I want to map to a destination
I have a view controller that I want to lazily initialize, and once initialized,
If I have Model.objects.all() I want to get only one object for any content_object=foo,
I have one list that I want to take a slice of, reverse that
i have one domain link text i want to know that does google crawl
I have one binary file and I want to read this file like first
Let's say that I have a boost::variant<std::string, int> myVariant; In this object I keep
I have a class, which has a boost::asio::io_service object. I want this object stored
I want to have one callback function after actions are done, I'm trying something
I want to have a one-click excel export feature for my application. I therefore

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.