2009-08-20 50 views
1

我正在经历一个奇怪的行为。 在视图控制器我想保留我的应用程序代理的参考([[UIApplication sharedApplication] delegate]self.appDelegate = [[UIApplication sharedApplication] delegate];总是0x0

所以我有一个属性,当我的控制器实例化时,我设置了。 所有工作在模拟器上的设备上,我的属性总是在调试器中为0x0。

我写了这个代码进行测试:

 

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{ 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
    if (self) 
    {  
     AppDelegate *localVar = [[UIApplication sharedApplication] delegate]; // OK 
     self.appDelegate = [[UIApplication sharedApplication] delegate]; // 0x0 
    } 
    return self; 
} 
 

我深信的appDelegate设置,我也写了二传,以验证它被称为

 

- (void)setAppDelegate:(AppDelegate *)delegate 
{ 
    appDelegate = delegate; // delegate is a valid address but appDelegate is still showing 0x0 
} 
 

我想知道如果我的AppDelegate类写得很好。

你有什么想法吗?

我迷路了......

感谢

回答

1

在您的视图控制器被加载时,代表可能尚未实例化或在UIApplication对象设置。所有这些都是在笔尖加载中完成的,但顺序是任意的。

2

赫斯的一些事情,你可以尝试 -

  1. 你如何申报您的self.appDelegate?它是AppDelegate的对象吗?
  2. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil中放置一个断点,看看self是什么。这是AppDelegate的实例吗?
  3. 此外,每当我想要得到的AppDelegate一个实例我做到这一点 - AppDelegate *localVar = (AppDelegate *)[[UIApplication sharedApplication] delegate];(注意的AppDelegate*类型转换)
  4. 我高度怀疑什么nikolia指出的是尽可能UIApplication委托是被实例化才的第一件事是你的viewController调用。

希望这会有所帮助...

相关问题