yue1liang1chuan 2013-01-11 06:14 采纳率: 0%
浏览 2402
已采纳

从应用delegate访问数组

在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中访问数组?

  • 写回答

1条回答 默认 最新

  • Matt 2013-01-11 23:32
    关注

    使用如下代码会得到全局共享的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这里呢...

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 汇编语言除法溢出问题
  • ¥65 C++实现删除N个数据列表共有的元素
  • ¥15 Visual Studio问题
  • ¥15 state显示变量是字符串形式,但是仍然红色,无法引用,并显示类型不匹配
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗