2011-05-16 24 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

相关问题