2012-03-17 16 views
0

Plsのはこれで私を助け:アクセスAppDelegateのインスタンスが、エラー:EXC_BAD_ACCESS

AppDelegateは、「利用者」と呼ばれる引数があります。

@interface AppDelegate : UIResponder <UIApplicationDelegate> 
{ 
    User *user; 
} 
@property (nonatomic, retain) User *user; 

を、私は延期のViewControllerにユーザーインスタンスを初期化:

User *userInfo = [[User alloc] initWithRealName:realName UserId:userId]; 

とAppDelegateのにユーザーを設定します。

AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate]; 
appDelegate.user = userInfo; 
を第二のViewControllerで

、私は使用者の本名を得ることができ、そこには問題はありません。そこ

AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate]; 
NSString *realName = appDelegate.user.realName; 

しかし、私は別のViewControllerにプッシュして、私はちょうど今のように、ユーザーの本名を取得したい、 けどエラーです:EXC_BAD_ACCESS:

AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate]; 
User *user = appDelegate.user; 
NSLog(@"I am in noticeDetailViewController:%@",user.realName);***//ERROR*** 

私は理由を知りたいのですか?このエラーを修正する方法について説明します。

ありがとうございます!

@interface AppDelegate : UIResponder <UIApplicationDelegate> { 
// ... 
@property (retain, nonatomic) User*user; 
// .. 

ドン:だから保持プロパティを作成することにより、AppDelegateを変更

NSLog(@"I am in noticeDetailViewController:%@",user.realName);***//ERROR*** 

:あなたがそれにアクセスしようとする前に

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 
+0

もユーザーのinitメソッドを変更しますか? – NeverBe

+0

イェップ、私はそれで確信しています。 – jxdwinter

答えて

0

AppDelegateuserを保持していないので、割り当て解除されますそれを合成するのを忘れないでください。ユーザーのプロパティを定義しなかった

- (id)initWithRealName:(NSString *)realNameArgument UserId:(NSString *)userIdArgument 
{ 
    self = [super init]; 
    if (self) { 
     self.realName = realNameArgument; 
     self.userId = userIdArgument;  
    } 
    return self; 
} 
+0

私はuser.hで@property(nonatomic、retain)User * userを作成し、それをUser.mで合成します。 – jxdwinter

+0

'User'のinitメソッドを変更しようとしましたか?また、 'user'だけを記録すると同じエラーが出ます:' NSLog(@ "私はnoticeDetailViewController:%@"、user); ' – sch

+0

...私は自己を忘れてしまいます。再度エラーは発生しません。どうもありがとう! – jxdwinter

関連する問題