简体   繁体   English

通过Objective-C中的类访问变量

[英]Access variable through classes in objective-c

In my project, my first view is a loging one, and I would like to get the username for example into others classes. 在我的项目中,我的第一个视图是登录视图,并且我想将用户名输入其他类。 I don't really know how I can get it in other classes, I searched in stackoverflow and didn't find (I tried several things but it didn't work) I give you how I tried to do this: 我真的不知道如何在其他类中获得它,我在stackoverflow中进行了搜索,但没有找到(我尝试了几件事,但是没有用),我给你我如何尝试做到这一点:

login.h 登录名

@interface loginViewController:UIViewController <UITextfieldDelegate>{

    IBOutlet UITextField *usernameField;
    IBOutlet UITextField *passwordField;
    IBOutlet UIButton *loginButton;
    NSString *user;

}
@property (nonatomic, retain) UITextField *usernameField;
@property (nonatomic, retain) UITextField *passwordField;
@property (nonatomic, retain) UIButton *loginButton;
@property (nonatomic, retain) NSString *user;
- (IBAction)login: (id) sender;
- (NSString *)user;

@end

login.m 登录名

@implementation LoginViewController

@synthesize usernameField;
@synthesize passwordField;
@synthesize loginButton;

- (IBAction) login: (id) sender{
    user=[[NSString alloc ]initWithFormat:@"%@",usernameField.text];
    //...I put here my login code...

    }

- (NSString *)user{
    return user;
    }

home.m

@implementation homeViewController
- (void)viewDidLoad 
{
[super viewDidLoad];
user2 = LoginViewController.user ;   //I tried this after the advice given below, still not working
user2 = LoginViewController.usernameField.text;   //same

NSLog(@"user: %@",user2);

}

I will need this value in all of my project, to display the informations about the client which is connected. 在我的所有项目中,都将需要此值,以显示有关所连接客户端的信息。

I just need a tip or a sample code I can work with. 我只需要可以使用的提示或示例代码。

Edit: I changed my code following the advices given, tell me if I missed something 编辑:我按照给出的建议更改了代码,告诉我是否错过了一些事情

Two things: 两件事情:

  1. Your main problem is that you declared a method that takes a sender argument ( getUser:(id)sender ), but are sending a message with no colon or arguments ( getUser ). 您的主要问题是,您声明了一个使用sender参数( getUser:(id)sender )的方法,但发送的消息中没有冒号或参数( getUser )。 Those are two totally different things. 那是完全不同的两件事。

  2. Accessors in Objective-C should not start with get — it means something else. Objective-C中的访问器不应以get开头,这意味着其他东西。 The selector (which is basically the Objective-C term for method names) should just be user . 选择器(基本上是方法名称的Objective-C术语)应该只是user So: 所以:

     - (NSString *)user { return user; } 

Create an object of your class loginViewController in homeViewController. 在homeViewController中创建类loginViewController的对象。

Then get or set the value of the login variable as per your requirment. 然后根据您的要求获取或设置登录变量的值。

Assuming that you need a NSString in your homeViewController,Create an object of loginViewController in homeViewController. 假设您的homeViewController中需要一个NSString,请在homeViewController中创建一个loginViewController对象。

anyRequiredVariable=login.usernameField.text;//Assuming that login is the object of loginViewController Class.

This will solve your problem. 这样可以解决您的问题。

Cheers 干杯

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM