-6

I need to access userEMail (NSString) property in Class2.m but its returning null.

Class1.h:

@interface AuthController : UIViewController 
{

    @public  NSString *const userPassword;
    @public  NSString *const userEMail;
    @public  NSString *const userFullName;

}

@property (nonatomic, strong) NSString *userEMail;

Then in Class1.m im saving userEMail.

Class2.m:

AuthController *ac = [[AuthController alloc] init];
NSLog(@"%@", ac.userEMail);
Rizier123
  • 58,877
  • 16
  • 101
  • 156

2 Answers2

-2

I just put basic step here, change it as per your requirement.

You just need to write

@property (nonatomic, strong) NSString *userEMail;

in your first.h file

write second.h file

@property (nonatomic, strong) NSString *gotUserEMail;

And your first.m file

you have gotUserEMail string with some value..

pass it to anotherViewController such liek,

secondViewController *addView = [[secondViewController alloc] init];
addView.gotUserEMail = userEMail;

.
.
.
.

EDITE

Okay then you need to use NSUserDefault.

NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setValue:userEMail forKey:@"myEmailString"];
[userDefaults synchronize];

get anywhere in your project by,

NSUserDefaults *data = [NSUserDefaults standardUserDefaults];  
NSString  *gotEmail = [data floatForKey:@"myEmailString"];
iPatel
  • 46,010
  • 16
  • 115
  • 137
-2

After reading a comment on another answer

I need to get userEMail value in multiple classes, so its wrong to send data to every class

It sounds more like you are after a singleton class. So try something like this

AuthObject.h

@interface AuthObject : NSObject 

@property (nonatomic, strong) NSString *userEMail;

+ (AuthController*)authInstance;

@end

AuthObject.m

#import "AuthObject.h"

@implementation AuthObject

@synthesize userEMail = _userEMail;

static AuthObject *authObjectSharedInstance = nil;

+ (AuthObject *)authInstance 
{
    static dispatch_once_t instanceToken;
    dispatch_once(&instanceToken, ^{
        authObjectSharedInstance = [[AuthObject alloc] init];
    });
    return authObjectSharedInstance;
}

@end

then in another method in another class as long as you have imported AuthObject.h somewhere you can do

- (void)someMethodIMadeUpInClass1
{
    AuthObject *authObj = [AuthObject authInstance];
    [authObj setUserEMail:@"myemail@address.com"];
}

then in a completely different class you can do

- (void)someMethodIMadeUpInClass2
{
    AuthObject *authObj = [AuthObject authInstance];
    NSLog(@"my email : %@", [authObj userEMail];
}

In theory if you're creating a singleton class and not going through a sharedInstance (authInstance here) then that code is broken. Attempting to hide that brokenness is just going to cause pain later on. This is why I would chose a singleton class over using NSUserDefaults.

Popeye
  • 11,839
  • 9
  • 58
  • 91
  • 1
    @downvoter Please give reason for downvote. I could give many examples (links, articles, etc) where a singleton would be perfect here. So why downvote? – Popeye Oct 25 '13 at 23:20
  • Again this has got another downvote but no reason has been left. Please provide a reason for the downvote and I can correct where possible. – Popeye Oct 02 '14 at 14:56