Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Objective-c, how to access an instance variable from another class

I am used to programming in Java and to use class variables to access data from other classes. Then I found out that class variables does not work the same way in Obj-C, and are having problems with that.

My problem is that i want to access the user inputted password in another class after the user is logged in. Have read in different forums and such that I should use class methods (+) to access these data. But because I need to create a new instance of the first class in class two it means that the inputted password does not exist in the new instance of class one.

My code is as follows:

class1.h

@interface class1 : UIViewController {
    UITextField *usernameField;
    UITextField *passwordField;
        UIButton *loginButton;
        NSString *password;
}

@property (nonatomic, retain) IBOutlet UITextField *usernameField;
@property (nonatomic, retain) IBOutlet UITextField *passwordField;
@property (nonatomic, retain) IBOutlet UIButton *loginButton;
@property (nonatomic, retain) NSString *password;

-(IBAction) loginButtonPushed;
+(NSString *)password;

@end

class1.m

#import "viewSwitcherViewController.h"

@implementation viewSwitcherViewController

@synthesize usernameField;
@synthesize passwordField;
@synthesize loginButton;
@synthesize password; // not needed

-(IBAction) loginButtonPushed {
        password = passwordField.text; //not needed
    // ...Code for switching view if successful login... EDITED:
    class2 *c2 = [class2 alloc] init]; //Instantiating new class2 object
    c2.password = passwordField.text; //Assigning tekst from passworField to thePassword-variable in the class2 instance.
}

class2.m

#import "class2.h"
#import "class1.h"

@implementation class2

@synthesize thePassword; //NSString variable

// this method is also not needed
-(void) someMethodUsingPassword {
         class1 *c1 = [[class1 alloc] init];
         thePassword = [c1 password];
         NSLog(@"The password is: %@", thePassword); //This call returns null
}

So my problem is that the c1 instance created in class2 does not hold the submitted password and therefore returns "null".

Maybe this is just my Java approach messing it up, but I couldn't find any other way so please help:)!

like image 309
Aleksander Akerø Avatar asked Mar 30 '11 15:03

Aleksander Akerø


People also ask

How do I get instance of another class?

There are two ways to access the instance variable of class: Within the class in instance method by using the object reference ( self ) Using getattr() method.

How do you access a variable within a class?

Accessing Class Variables We can access static variables either by class name or by object reference, but it is recommended to use the class name. Access inside the constructor by using either self parameter or class name. Access from outside of class by using either object reference or class name.

How do you call an instance method in Objective C?

YourClass *object = [[YourClass alloc] init]; [object someInstanceMethod]; The above is an instance method, because you must create an instance of YourClass (in this case, the object variable) before you can call the method " someInstanceMethod ".


2 Answers

A public class variable in Java is equivalent to a global variable in C and Objective-C. You would implement it as:

class1.h

extern NSString* MyGlobalPassword;

class1.m

NSString* MyGlobalPassword = nil;

Then, anyone that imports class1.h can read and write to MyGlobalPassword.

A slightly better approach would be make it accessible via a "class method".

class1.h:

@interface Class1 : UIViewController {
    UITextField *usernameField;
    UITextField *passwordField;
    UIButton *loginButton;    
}
+ (NSString*)password;
@end

class1.m:

static NSString* myStaticPassword = nil;

@implementation Class1
+ (NSString*)password {
    return myStaticPassword;
}

- (void)didClickLoginButton:(id)sender {
    [myStaticPassword release];
    myStaticPassword = [passwordField.text retain];
}

Other classes would then read the password through the password class method.

Class2.m :

-(void) someMethodUsingPassword {
     thePassword = [Class1 password]; // This is how to call a Class Method in Objective C
     NSLog(@"The password is: %@", thePassword); 
}
like image 97
Darren Avatar answered Sep 28 '22 07:09

Darren


You should rethink how your app structure is set up. Your basic need is that you need to save your password in one class and access it in another, right? NSUserDefaults is perfect (almost perfect, see note #3 below) for this. Change your class1 code to look like this:

-(IBAction) loginButtonPushed {
    NSString *username = self.usernameField.text;
    NSString *password = self.passwordField.text;

    // Do your login stuff here, if successful do the following
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefauls];
    [defaults setObject:username forKey:@"usernameKey"];
    [defaults setObject:password forKey:@"passwordKey"];
}

Get rid of your password property, you don't need it for anything now. Also, get rid of your +(NSString *)password; class method.

When you need to access the login and password:

-(void) someMethodUsingPassword {
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefauls];
    NSString *username = [defaults stringForKey:@"usernameKey"];
    NSString *password = [defaults stringForKey:@"passwordKey"];

    // do whatever here
}

A few notes

  1. I wrote this from memory, so they may have errors if you spot any let me know and I'll update my answer.
  2. You seem to be missing a few key points about how to use class methods and properties. I highly recomend reading Apple's Introduction to Objective-C.
  3. Storing login and password information in NSUserDefaults is NOT secure. If someone gains root access to your device or to your device backups, they may be able to extract this login information. If security is crucial to you, store the login information in Keychain.
like image 40
kubi Avatar answered Sep 28 '22 06:09

kubi