2011-05-16 27 views
1

在我的AppDelegate我有這樣一個NSObject的一個參考:UIApplicationDelegate問題

@interface MyAppDelegate : NSObject <UIApplicationDelegate> 
{ 
    MyObjectManager * myObjectManager; 
} 

@property (nonatomic, retain) IBOutlet UIWindow *window; 
@property (nonatomic, retain) IBOutlet UINavigationController *navigationController; 

@property (nonatomic, retain) MyObjectManager * myObjectManager; 

我想從我的UIViewController訪問此,所以我這樣做:

MyAppDelegate * appDelegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate]; 
MyObjectManager * myObjectManager = appDelegate.myObjectManager; 
maMyArray = [[NSMutableArray alloc] init]; 

[[appDelegate myObjectManager] findMyStuff:5 foundArray:maMyArray]; 
//[myObjectManager findMyStuff:5 foundArray:maMyArray]; 

不過,我覺得我不正確理解Objective C中的語法,因爲我引發了異常EXC_BAD_ACCESS。當我看着這些價值觀時,他們看起來是正確的。

有人能解釋我做錯了什麼嗎?

感謝

回答

1

您必須初始化myObjectManager somwhere:

myObjectManager = [[MyObjectManager alloc] init]; 

最好的地方是大概在- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions方法。

不要忘記在dealloc中釋放它。

+0

我是的,在MyAppDelegate中。我這樣做:MyObjectManager = [[MyObjectManager alloc] initWithContentsOfSQLiteDB:@「MyDB.sql」];我還需要做些別的嗎? – LilMoke 2011-05-16 20:45:41

+0

哦,我明白了,好的,但是我在哪裏可以調用init來做我需要做的事情?我可以在我的init函數中調用[[self alloc] init]嗎? – LilMoke 2011-05-16 20:47:55

+0

只要那是myObjectManager =,而不是MyObjectManager =,你的代碼也應該正常工作。如果你在findMyStuff:方法調用之前放置一個斷點,myObjectManager指向什麼? – Joe 2011-05-16 20:52:28

2

哪裏是分配MyObjectManager並將其分配給您的myObjectManager屬性的代碼?

直到你這樣做,它是零。

+0

在MyAppDelegate ...其他評論。 – LilMoke 2011-05-16 20:46:34

+0

將代碼添加到你的原始問題 – 2011-05-16 20:54:33

相關問題