Objective-C,如何从另一个类访问实例变量
我习惯于用Java编程并使用类变量来访问其他类中的数据.然后我发现类变量在Obj-C中的工作方式不同,并且存在问题.
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.
我的代码如下:
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
}
所以我的问题是,在class2中创建的c1实例不保存提交的密码,因此返回"null".
So my problem is that the c1 instance created in class2 does not hold the submitted password and therefore returns "null".
也许这只是我的Java方法搞砸了,但是我找不到其他方法,请帮忙:)!
Maybe this is just my Java approach messing it up, but I couldn't find any other way so please help:)!
Java中的公共类变量等效于C和Objective-C中的全局变量.您可以将其实现为:
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;
然后,任何导入class1.h的人都可以读写MyGlobalPassword
.
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];
}
然后其他类将通过password
类方法读取密码.
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);
}