簡體   English   中英

從另一個類Objective-C獲取數組

[英]Getting an array from another class objective-C

我有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)

當你這樣做時會發生什么:

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];

在viewController中添加一個公共方法,一旦你的xml解析完成,調用它意味着的公共方法,你告訴你的viewController數據已經准備好並更新UI。

您可以使用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]
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM