从应用程序委托访问数组

Rav*_*ale 0 iphone nsxmlparser ios

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

@property(strong,nonatomic)NSMutableArray *books;  
Run Code Online (Sandbox Code Playgroud)

我在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;
}
Run Code Online (Sandbox Code Playgroud)

我已经在XMLParser.h中声明了这个数组

@interface XMLParser : NSObject {
    NSMutableString *currentElementValue;

    AppDelegate *appDelegate;
    Book *aBook; 
}
Run Code Online (Sandbox Code Playgroud)

现在我必须在BooksDetailViewController.m中访问此数组,如何访问此数组.简而言之,我如何从应用程序委托访问数组到BooksDetailViewController

Meh*_*tri 5

首先你应该allocinit你的数组,然后你可以在其中添加对象.

你应该在这样创建AppDelegate实例BooksDetailViewController,

在.h文件中

AppDelegate *appDelegate;
Run Code Online (Sandbox Code Playgroud)

并在.m文件中

appDelegate = [[UIApplication sharedApplication]delegate];
Run Code Online (Sandbox Code Playgroud)

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

appDelegate.books
Run Code Online (Sandbox Code Playgroud)