从应用程序委托访问数组

时间:2013-01-10 11:25:55

标签: iphone ios nsxmlparser

我在应用程序委托中声明了1个数组

@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中访问这个数组,我该如何访问这个数组。 简而言之,我如何从应用程序委托访问数组到BooksDetailViewController

1 个答案:

答案 0 :(得分:5)

首先,您必须拥有allocinit数组,然后您可以在其中添加对象。

你必须在BooksDetailViewController这样创建AppDelegate实例,

<。>文件中的

AppDelegate *appDelegate;

和.m文件

appDelegate = [[UIApplication sharedApplication]delegate];

现在您可以像这样访问您的数组,

appDelegate.books