从另一个类Objective-C获取数组

时间:2012-10-15 15:00:05

标签: objective-c ios nsarray

我有xml文件,我在xml文件中搜索所有信息并存储在数组中,

这是我的starDidElement文件,用于存储在数组中:

-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI 
 qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{

if ([elementName isEqualToString:@"Presentation"]) {
    NSLog(@"user element found – create a new instance of User class...");
    app.presentationArray = [[NSMutableArray alloc] init];
    thePresentation = [[Presentation alloc] init];
    thePresentation.pLabel = [attributeDict objectForKey:@"label"];
    NSLog(@"PLabel: %@", thePresentation.pLabel);

}else if ([elementName isEqualToString:@"slides"]) {
    NSLog(@"Slides");

    thePresentation.slides = [NSMutableArray array];

在我的标题中我有

   Presentation thePresentation;

请你帮我解决这个问题

提前致谢

修改:

Presentation *aPresentation = [app.presentationArray objectAtIndex:0];
NSLog(@"Presentation is: %@ and it's Slide Count is: %d",aPresentation.pLabel, aPresentation.slides.count);

Slide *aSlide = [aPresentation.slides objectAtIndex:0];
NSLog(@"Slide Label is: %@", aSlide.sLabel);

UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setTitle:[NSString stringWithString:aSlide.sLabel] forState:UIControlStateNormal];
btn.frame = rect;
[btn setTag:i];
[btn addTarget:self action:@selector(buttonClick:)  forControlEvents:UIControlEventTouchUpInside];
[imageView addSubview:btn];

然后日志

[788:c07]演示文稿是:( null)它的幻灯片计数是:0 [788:c07]幻灯片标签是:( null)

2 个答案:

答案 0 :(得分:2)

当你这样做时会发生什么:

Presentation *aPresentation = [app.presentationArray objectAtIndex:0];
NSLog(@"Presentation is: %@ and it's Slide Count is: %d",aPresentation.pLabel, aPresentation.slides.count);

Slide *aSlide = [aPresentation.slides objectAtIndex:0];
    NSLog(@"Slide Label is: %@", aSlide.sLabel);

    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setTitle:[NSString stringWithString:aSlide.sLabel] forState:UIControlStateNormal];
    btn.frame = rect;
    [btn setTag:i];
    [btn addTarget:self action:@selector(buttonClick:)  forControlEvents:UIControlEventTouchUpInside];
    [imageView addSubview:btn];

答案 1 :(得分:0)

在你的viewController中添加一个公共方法,一旦你的xml解析完成,调用它意味着的公共方法,你告诉你的viewController数据已准备就绪&更新用户界面。

您可以使用appDelegate通知viewController数据已准备就绪,因为appDelegate将拥有您的viewController实例。

编辑:您可以拥有以下内容:

//in viewController.h
- (void) viewFillUp;

//in viewController.m
-(void)viewFillUp
{
      Slide *aSlide = [thePresentation.slides objectAtIndex:0];
      NSLog(@"Slide Label is: %@", aSlide.sLabel);

      UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
      [btn setTitle:[NSString stringWithString:aSlide.sLabel] forState:UIControlStateNormal];
      btn.frame = rect;
      [btn setTag:i];
      [btn addTarget:self action:@selector(buttonClick:)  forControlEvents:UIControlEventTouchUpInside];
      [imageView addSubview:btn];
}

//in appDelegate.h
 - (void) parsingDone;

//in appDelegate.m
- (void) parsingDone
{
    //suppose ur viewController instance is self.viewController
    [self.viewController viewFillUp]
}