[英]How to access @public instance variable from another class in Objective-C?
[英]Objective-c, how to access an instance variable from another class
我习惯用Java编程并使用类变量来访问其他类的数据。 然后我发现类变量在Obj-C中的工作方式不同,并且遇到了问题。
我的问题是我想在用户登录后访问另一个类中的用户输入密码。已经阅读了不同的论坛,我应该使用类方法(+)来访问这些数据。 但是因为我需要在第二类中创建第一个类的新实例,这意味着输入的密码不存在于第一类的新实例中。
我的代码如下:
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”。
也许这只是我的Java方法弄乱它,但我找不到任何其他方式所以请帮助:)!
Java中的公共类变量等同于C和Objective-C中的全局变量。 您可以将其实现为:
class1.h
extern NSString* MyGlobalPassword;
class1.m
NSString* MyGlobalPassword = nil;
然后,任何导入class1.h的人都可以读写MyGlobalPassword
。
稍微好一点的方法是通过“类方法”使其可访问。
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
类方法读取密码。
Class2.m:
-(void) someMethodUsingPassword {
thePassword = [Class1 password]; // This is how to call a Class Method in Objective C
NSLog(@"The password is: %@", thePassword);
}
您应该重新考虑应用程序结构的设置方式。 您的基本需求是您需要将密码保存在一个类中,然后在另一个类中访问它,对吗? NSUserDefaults
非常完美(几乎完美,请参见下面的注释#3)。 将您的class1
代码更改为如下所示:
-(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"];
}
摆脱你的密码属性,你现在不需要它。 另外,摆脱你的+(NSString *)password;
类方法。
当您需要访问登录名和密码时:
-(void) someMethodUsingPassword {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefauls];
NSString *username = [defaults stringForKey:@"usernameKey"];
NSString *password = [defaults stringForKey:@"passwordKey"];
// do whatever here
}
几点说明
NSUserDefaults
存储登录名和密码信息是不安全的。 如果有人获得对您的设备或设备备份的root访问权限,他们可能能够提取此登录信息。 如果安全性对您至关重要,请将登录信息存储在Keychain中 。 本class1
中创建class2
不会有密码设置,因为你只是实例化一个新的实例[[class1 alloc] init]
-这不会与其他实例共享成员变量class1
其它地方产生。
通常,
当您在一个屏幕上接受密码并对其执行任何操作时,您仍然拥有密码。 当你移动到另一个屏幕时为什么不保留,class1的对象作为class2中的属性并在你将控件传递给class 2之前设置它。
或者,如果您只是调用class2的方法,则将class1 obj作为已包含密码的参数传递给它。
如果你只是将它作为参数传递给方法,你可以访问它。 或者,如果您将其设置为此class2的属性。 那么你总是可以使用一个吸气剂。
这件事与Java或Objective C无关。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.