Objective-c,如何从另一个类访问实例变量

发布于 2024-10-29 00:16:12 字数 1788 浏览 3 评论 0原文

我习惯于用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

巷雨优美回忆 2024-11-05 00:16:12

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); 
}

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); 
}
云雾 2024-11-05 00:16:12

您应该重新考虑如何设置应用程序结构。您的基本需求是您需要将密码保存在一个类中并在另一类中访问它,对吧? 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; class 方法。

当您需要访问登录名和密码时:

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

    // do whatever here
}

一些注释

  1. 我凭记忆写了
  2. ,因此如果您发现任何错误,请告诉我,我会更新我的答案。您似乎缺少有关如何使用类方法和属性的一些关键点。我强烈推荐阅读 Apple 的 Objective-C 简介
  3. 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 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.
面如桃花 2024-11-05 00:16:12

class2 中创建的 class1 不会设置密码,因为您刚刚使用 [[class1 alloc] init] 实例化了一个新实例 - - 这不会与其他地方创建的 class1 的其他实例共享成员变量。

The class1 created in class2 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 of class1 created elsewhere.

魂归处 2024-11-05 00:16:12

通常,

当您在一个屏幕上接受密码并对它进行任何操作时,您仍然可以使用。
当您移动到另一个屏幕时,为什么不保留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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文