更新 appDelegate 中的变量
在应用程序委托中,我创建了一个数组供多个视图控制器引用。但是,当我尝试在一个视图中更新数组时,它在另一个视图中显示为空。
视图 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可能会在原始填充的数组上重新初始化该数组。我会在你正在做的地方放置一个断点
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
and see if it's being called more often then you thought.
我怀疑您尚未初始化委托中的 myData 数组。你应该
在你的
方法中调用。否则,您从委托返回的指针不会指向 NSMutableArray 的实例,
然后我将与调试器检查您是否没有返回 nil,并且您返回的是相同的地址。如果其中任何一个不正确,我怀疑事情发生的顺序就是问题所在
I suspect that you have not initialized the myData array in the delegate. You should be calling
in your
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