如何将用户信息从登录视图传递到另一个视图控制器

时间:2013-01-14 21:11:08

标签: login ios6 xcode4.5

我有一个Login视图控制器,我想获取用户名和地址以及联系号码,并将其发布在另一个视图控制器上。我可以使用一些有关尝试的建议。

1 个答案:

答案 0 :(得分:1)

您可以在委托文件中创建一个具有用户登录凭据的登录对象:

#import "Login.h"  

@interface YourAppDelegate : UIResponder{

Login *appLogin;

}

@property(nonatomic,retain) Login *appLogin; // Don't forget to release it in the dealloc method

然后你的Login.h文件看起来像这样:

@interface Login : NSObject <NSCoding> {

NSString *userName; 
NSString *address;
NSString *contactNumber;

// You can add more properties
}

@property (nonatomic, retain)NSString *userName;
@property (nonatomic, retain)NSString *address;
@property (nonatomic, retain)NSString *contactNumber;

- (id)initWithUserName:(NSString *)name andAddress:(NSString *)addr andContactNumber:(NSString *)cNumber;

实现(Login.m)文件如下所示:

@implementation Login

@synthesize userName;
@synthesize address;
@synthesize contactNumber;

- (id)initWithUserName:(NSString *)name andAddress:(NSString *)addr andContactNumber:(NSString *)cNumber{

if(self = [super init]){

     [self setUserName:name];
     [self setAddress:addr];
     [self setContactNumber:cNumber];
}

return self;   
}

使用dealloc方法释放您的属性 最后在您的登录视图中,您可以像这样分配appDelegate登录对象:

YourAppDelegate *delegate = [[UIApplication sharedApplication] delegate];
Login *lObject = [[Login alloc] initWithUserName:@"user name here" andAddress:@"Address here" andContatNumber@"Contact number here"];

[delegate setAppLogin:lObject];
[lObject release];

然后在视图控制器中,您希望显示此凭据执行以下操作:

YourAppDelegate *delegate = [[UIApplication sharedApplication] delegate];
NSString *userName = [delegate.appLogin userName];