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

  • Home
  • SEARCH
  • 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 7797839
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T23:49:04+00:00 2026-06-01T23:49:04+00:00

Started looking around for a global static, to set a color in one place,

  • 0

Started looking around for a global static, to set a color in one place, to be used throughout an app. I couldn’t understand some very good answers (older) about the singleton here on SO, so I created a class to very simply handle this. Based on some (of the other threads), I decided to avoid the app delegate.

There seems to be several ways to handle this. As a low experience ios/objective-c developer, what does the method below miss? (It works, by the way and seems simple.)

// Global.h
@interface Global : NSObject

@property (strong, nonatomic) UIColor *myColor;

- (id)initWithColor:(NSString *)color;

// Global.m
@implementation Global

@synthesize myColor;

- (id)initWithColor:(NSString *)color
{
    if (!self) {
        self = [super init];
    }

    if ([color isEqualToString:@"greenbackground"])
        myColor = [[UIColor alloc] initWithRed:0.0 / 255 green:204.0 / 255 blue:51.0 / 204 alpha:1.0];
    ... and so on with other colors
    return self;
}
@end

Later to use the color:

cell.backgroundColor = [[[Global alloc] initWithColor:@"greenbackground"] myColor];

Edited for better solution:

// Global.h

#import <foundation/Foundation.h>

@interface Global : NSObject {
    UIColor *myGreen;
    UIColor *myRed;
}

@property (nonatomic, retain) UIColor *myGreen;
@property (nonatomic, retain) UIColor *myRed;

+ (id)sharedGlobal;

@end

// Global.m
#import "Global.h"

static Global *sharedMyGlobal = nil;

@implementation Global

@synthesize myGreen;
@synthesize myRed;

#pragma mark Singleton Methods
+ (id)sharedGlobal {
    @synchronized(self) {
        if (sharedMyGlobal == nil)
            sharedMyGlobal = [[self alloc] init];
    }
    return sharedMyGlobal;
}

- (id)init {
    if (self = [super init]) {
        myGreen = [[UIColor alloc] initWithRed:0.0 / 255 green:204.0 / 255 blue:51.0 / 204 alpha:1.0];
        myRed = [[UIColor alloc] initWithRed:204.0 / 255 green:51.0 / 255 blue:51.0 / 204 alpha:1.0];
    }
    return self;
}

@end

And usage:

cell.comboLabel.textColor = [[Global sharedGlobal] myGreen];
  • 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-01T23:49:07+00:00Added an answer on June 1, 2026 at 11:49 pm

    Well, calling alloc and init all the time is wasteful if you want to use your color properties globally. This is where Singletons come to help in that you create it only ONCE (alloc + init) and than use it wherever you want in your code.

    In your case alloc is called every time you want to read myColor which is wasteful considering you are going to use it throughout your code.

    this looks much cleaner :

    cell.backgroundColor = [[Global sharedInstance] myColor];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I started looking around for a search engine and after some reading I decided
I've recently started looking at Merb, for use with some small projects around the
I've just started messing around with Windows Phone development, and I'm looking for some
I started looking into PhoneGap yesterday and created a simple marble rolling around while
Just started looking at some MVC JS framework / library and most of them
I've started looking into a few larger server-app frameworks that would be able to
I've started looking at re-writing some pages on a clients website and I can
I have recently started looking into using Azure but I'm having some issues getting
Just started to play around with node.js, have some past experience with JavaScript but
I've just started using Thor. I've been looking around the documentation and can't find

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.