gpt4 book ai didi

iphone - 从不同的 .xib 文件访问 App Delegate 信息

转载 作者:行者123 更新时间:2023-11-28 19:20:34 25 4
gpt4 key购买 nike

我有一个带有 App Delegate 的 .xib 文件。在这个 App Delegate 中,我有一些变量实例:

//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 Delegate 中,我使用这个变量执行了一些操作。更具体地说,我将一些图像分配给 _images 变量。根据调试器,信息已正确存储。

在不同的 .xib 文件中,我想访问这个委托(delegate)的内容。所以我的想法是通过使用这个函数来访问整个委托(delegate):

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

我可以看到委托(delegate)变量被实例化了,包括对所有变量的访问。但是,在处理它们的时候,变量是空的(之前.xib中生成的值好像没有存储)。

有什么想法或建议吗?有没有更好的方法从不同的 .xib 文件访问以前的委托(delegate)?

最佳答案

app delegate 不是全局变量的中央存储位置。将此数据移动到单例模型对象中(从外观上看,它类似于 Poem)。您可以从任何需要它的对象访问 [Poem sharedPoem]。或者,您可以在应用委托(delegate)中实例化单个 Poem 对象,并将其交给需要它的各种 View Controller 。每种方法都有其优势。

有关正确使用应用委托(delegate)和模型对象的更多讨论,请参阅以下问题:

关于iphone - 从不同的 .xib 文件访问 App Delegate 信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9230858/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com