Pls help me with this:
The AppDelegate has an argument called “user”:
@interface AppDelegate : UIResponder <UIApplicationDelegate>
{
User *user;
}
@property (nonatomic, retain) User *user;
and I init the user instance in the frist viewController:
User *userInfo = [[User alloc] initWithRealName:realName UserId:userId];
and set the user to AppDelegate’s:
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
appDelegate.user = userInfo;
In the second viewController, I can get the use’s realName,there is no problem:
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
NSString *realName = appDelegate.user.realName;
But when I push to another viewController,and I want to get the user’s realName like just now,
but there is an error:EXC_BAD_ACCESS:
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
User *user = appDelegate.user;
NSLog(@"I am in noticeDetailViewController:%@",user.realName);***//ERROR***
And I want to know why? and how to fix this error.
Thanks!
User.h & User.m
@interface User : NSObject
{
NSString *realName;
NSString *userId;
}
@property (nonatomic, retain)NSString *realName;
@property (nonatomic, retain)NSString *userId;
- (id)initWithRealName :(NSString *)realNameArgument UserId :(NSString *)userIdArgument;
@end
@implementation User
@synthesize realName,userId;
- (id)initWithRealName :(NSString *)realNameArgument UserId :(NSString *)userIdArgument
{
self = [super init];
if (self)
{
realName = realNameArgument;
userId = userIdArgument;
}
return self;
}
- (void)dealloc
{
[super dealloc];
[realName release];
[userId release];
}
@end
AppDelegatedoes not retainuser, so it gets deallocated before you try to access it in:So Change
AppDelegateby creating a retained property:And don’t forget to synthesize it.
Also change the init method of user to: