2012-02-10 25 views
0

我有一個帶有App Delegate的.xib文件。在這個應用程序代表,我有變數某些情況下:訪問應用程序從不同的.xib文件中委派信息

//The model objects 
NSString* _stringWord; 
NSMutableArray *_images; 
int _numberOfWordsTotal; 
int _numberOfWords; 

哪些是方便聲明爲屬性:

@property (nonatomic, copy) NSString * stringWord; 
@property (nonatomic) int numberOfWordsTotal; 
@property (nonatomic) int numberOfWords; 
@property (nonatomic, copy) NSMutableArray * images; 

而在.m文件

@synthesize stringWord = _stringWord; 
@synthesize images = _images; 
@synthesize numberOfWordsTotal = _numberOfWordsTotal; 
@synthesize numberOfWords = _numberOfWords; 

合成在APP委託我用這個變量執行一些操作。更具體地說,我將一些圖像分配給_images變量。根據調試器,信息被正確存儲。

在另一個.xib文件中,我想訪問此代理的內容。所以我的想法是通過使用該功能來訪問整個委託:

Visual_PoetryAppDelegate *delegate = [[UIApplication sharedApplication] delegate]; 

我可以看到,委託變量被實例化,包括獲得所有的變量。但是,當涉及到處理它們時,變量是空的(在以前的.xib中生成的值似乎沒有被存儲)。

任何想法或建議?有沒有更好的方法從不同的.xib文件訪問前一個委託?

回答

1

更改複印保留

@property (nonatomic, retain) NSString * stringWord; 
@property (nonatomic) int numberOfWordsTotal; 
@property (nonatomic) int numberOfWords; 
@property (nonatomic, retain) NSMutableArray * images; 
3

應用程序委託並不是全局變量的中央存儲位置。將這些數據移動到一個單例模型對象中(通過它的外觀,它會像Poem)。您可以從任何需要它的對象訪問[Poem sharedPoem]。或者,您可以在應用程序委託中實例化一個對象,並將其交給需要它的各種視圖控制器。每種方法都有其優點。

中查看討論正確使用應用程序委託和模型對象的下列問題:

相關問題