views:

52

answers:

1

I have a class called 'Constants' that I am storing a String variable in. This class contains a few global variables used in my app.

I want to be able to reference this class and call the variable (called profileId) in other Views of my app.

I looked around and found a few examples, but am not sure how to do this. Currently my setup is:

Constants.h

@interface Constants : UIViewController {
NSString *profileId;
}

@property (nonatomic, retain) NSString *profileId;

@end

Constants.m

#import "Constants.h"

@implementation Constants

@synthesize profileId;

- (void)dealloc {
[profileId release];

[super dealloc];
}

And I am trying to call the variable profileId in a new View via this way:

NewView.h file

@class Constants;

NewView.m file

NSLog(@"ProfileId is:", [myConstants profileId]);

Is there something I'm missing? It is coming up null, even though I am properly storing a value in it in another function via this way:

Constants *Constant;
    Constant = [[Constants alloc] init];
    Constant.profileId = userId;
+1  A: 

You are missing the %@ for the parameter:

NSLog(@"ProfileId is: %@", [myConstants profileId]);

As a side note, variable names should begin with a lower case letter (constant, not Constant). You also can use dot syntax with properties here: myConstants.profileId

If this doesn't work, please post the code that you use to assign your value (complete method).

Eiko
I tried your code, and it's the value comes out null...For assigning my value, I do this:In my .h file:#import "Constants.h"@class Constants;In my .m file:NSString *responseString = [request responseString]; NSString *userId = [responseString stringBetween:@"<userid>" and:@"</userid>"]; NSLog(@"User Id is:"); NSLog(@"%@",userId);(userId displays a correct value).Constants *Constant; Constant = [[Constants alloc] init]; Constant.profileId = userId; NSLog(@"Constant profileId is:"); NSLog(@"%@",Constant.profileId);(Constant.profileId displays the correct value).
squeezemylime
I solved the problem via this code:http://iphone.galloway.me.uk/iphone-sdktutorials/singleton-classes/
squeezemylime
You should try to find and understand your error, as you will probably doing similar things again and again. But without the code (full methhod) we can't help. You can always edit your question.
Eiko