在delegate中声明了一个数组:@property(strong,nonatomic)NSMutableArray *books;
然后在XMLParser.m中给这个数组添加对象:
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
if([elementName isEqualToString:@"Books"])
return;
NSLog(@"i m in didEndElement");
if([elementName isEqualToString:@"Book"]) {
[appDelegate.books addObject:aBook]; //here i have added objects to array
[aBook release];
aBook = nil;
NSLog(@"books=%@",appDelegate.books);
}
else
[aBook setValue:currentElementValue forKey:elementName];
[currentElementValue release];
currentElementValue = nil;
}
在XMLParser.h中声明数组:
@interface XMLParser : NSObject {
NSMutableString *currentElementValue;
AppDelegate *appDelegate;
Book *aBook;
}
现在需要访问在BooksDetailViewController.m
中的数组,怎么实现?如何从应用delegate
中访问数组?
使用如下代码会得到全局共享的AppDelegate
[[UIApplication sharedApplication] delegate]
你在向这个array
里添加object
的时候应该这么写
AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
[appDelegate.books addObject:aBook];
在BooksDetailViewController.m
中读取时这么写
AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
id book = [appDelegate.books objectAtIndex:0];
但是我想问一下,你怎么把bookArray
放到appDelegate
这里呢...
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。