Objective-c,如何从另一个类访问实例变量
我习惯于用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 方法搞砸了,但我找不到任何其他方法,所以请帮助:)!
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:)!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
Java 中的公共类变量相当于 C 和 Objective-C 中的全局变量。您可以将其实现为:
class1.h
class1.m
然后,任何导入 class1.h 的人都可以读取和写入
MyGlobalPassword
。一个稍微好一点的方法是通过“类方法”访问它。
class1.h:
class1.m:
其他类将通过
password
类方法读取密码。Class2.m:
A public class variable in Java is equivalent to a global variable in C and Objective-C. You would implement it as:
class1.h
class1.m
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:
class1.m:
Other classes would then read the password through the
password
class method.Class2.m :
您应该重新考虑如何设置应用程序结构。您的基本需求是您需要将密码保存在一个类中并在另一类中访问它,对吧?
NSUserDefaults
对此来说是完美的(几乎完美,请参见下面的注释#3)。将您的class1
代码更改为如下所示:删除您的密码属性,您现在不需要它。另外,删除您的
+(NSString *)password;
class 方法。当您需要访问登录名和密码时:
一些注释
NSUserDefaults
中存储登录名和密码信息并不安全。如果有人获得了您的设备或设备备份的 root 访问权限,他们也许能够提取此登录信息。如果安全对您来说至关重要,请将登录信息存储在 Keychain。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 yourclass1
code to look like this: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:
A few notes
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.在
class2
中创建的class1
不会设置密码,因为您刚刚使用[[class1 alloc] init]
实例化了一个新实例 - - 这不会与其他地方创建的class1
的其他实例共享成员变量。The
class1
created inclass2
won't have a password set, because you just instantiated a new instance with[[class1 alloc] init]
-- this will not share member variables with other instances ofclass1
created elsewhere.通常,
当您在一个屏幕上接受密码并对它进行任何操作时,您仍然可以使用。
当您移动到另一个屏幕时,为什么不保留Class1的对象为Class2中的属性,然后在将控件传递给2类之前将其设置为2
。已经在其中。
如果您只将其作为参数传递给该方法,则可以访问它。
或者如果将其设置为此类2的属性。
那么你总是可以使用吸气剂。
这东西与Java或Objective C无关。
Generally,
When you accept the password in one screen and do whatever with it, you still have it.
When you are moving to someother screen why dont you keep, class1's object as a property in class2 and set it before u pass the control to class 2.
Or if you are just calling the method of class2 ust pass class1 obj as a parameter with password already in it.
and in case u just pass it to the method as a parameter, u can access it.
Or if you are setting it as a property of this class2.
then u can always use a getter.
This thing has got nothing t do with Java or Objective C.