在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這裡呢...