更新 appDelegate 中的变量

发布于 2024-08-10 12:20:54 字数 1206 浏览 5 评论 0原文

在应用程序委托中,我创建了一个数组供多个视图控制器引用。但是,当我尝试在一个视图中更新数组时,它在另一个视图中显示为空。

视图 1:

MyAppDelegate *appDelegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];
dataTempForSearch = appDelegate.myData;

然后我有一个 xml 解析器,它将一个对象插入到数组中。

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
    if ([elementName isEqualToString:@"CompanyName"]) {
        // save values to an item, then store that item into the array...
        [item setObject:currentTitle forKey:@"title"];
        [item setObject:currentAddress forKey:@"address"];
        [item setObject:currentCity forKey:@"city"];
        [item setObject:currentCountry forKey:@"country"];

        [dataTempForSearch addObject:[item copy]];
    }
}

这在视图 1 中返回一切正常,但在视图 2 中我有:

- (void)viewDidLoad {
    MyAppDelegate *appDelegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];
    dataTempForMap = appDelegate.myData;

    NSLog(@"myData appDelegate from: %@", dataTempForMap);      
}

加载第二个视图时,dataTempForMap 返回一个空数组。

In the application delegate, I created an array for multiple view controllers to reference. However, when I try to update the array in one view, it comes up empty in the other.

View 1:

MyAppDelegate *appDelegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];
dataTempForSearch = appDelegate.myData;

I then have an xml parser that inserts an object into the array.

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
    if ([elementName isEqualToString:@"CompanyName"]) {
        // save values to an item, then store that item into the array...
        [item setObject:currentTitle forKey:@"title"];
        [item setObject:currentAddress forKey:@"address"];
        [item setObject:currentCity forKey:@"city"];
        [item setObject:currentCountry forKey:@"country"];

        [dataTempForSearch addObject:[item copy]];
    }
}

This returns everything fine in view 1, but in view 2 I have:

- (void)viewDidLoad {
    MyAppDelegate *appDelegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];
    dataTempForMap = appDelegate.myData;

    NSLog(@"myData appDelegate from: %@", dataTempForMap);      
}

when this second view is loaded, the dataTempForMap returns an empty array.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

心房敞 2024-08-17 12:20:54

您可能会在原始填充的数组上重新初始化该数组。我会在你正在做的地方放置一个断点

myData = [[NSMutableArray alloc] init];

and see if it's being called more often then you thought.

It's possible that you are reinitializing the array over the original populated one. I'd put a breakpoint on the spot where you're doing

myData = [[NSMutableArray alloc] init];

and see if it's being called more often then you thought.

抠脚大汉 2024-08-17 12:20:54

我怀疑您尚未初始化委托中的 myData 数组。你应该

[[NSMutableArray alloc]init];  

在你的

- (void)applicationDidFinishLaunching:(UIApplication *)application {

方法中调用。否则,您从委托返回的指针不会指向 NSMutableArray 的实例,

然后我将与调试器检查您是否没有返回 nil,并且您返回的是相同的地址。如果其中任何一个不正确,我怀疑事情发生的顺序就是问题所在

I suspect that you have not initialized the myData array in the delegate. You should be calling

[[NSMutableArray alloc]init];  

in your

- (void)applicationDidFinishLaunching:(UIApplication *)application {

method. Otherwise the pointer you are getting back from the delegate is not pointing to an instance of NSMutableArray

I would then check with the debugger that you are not getting nil back and that you are getting the same address back. If either of these are not true I suspect that order in which things is happening is the problem

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文