2012-10-15 31 views
0

我的XML文件,我RAED在xml文件,並存儲在陣列中的所有信息的數組,獲得從其它類目標-C

這裏是在陣列中存儲我的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; 

請你幫我inpelement這個

感謝推進

編輯:

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]演示是:(空)和它的幻燈片Count是:0 [788:C07]幻燈片標籤是:(null)

+0

你這樣做基本上是相同的方式,你會做它在Java中。 –

+0

@HotLicks你可以寫你的代碼在答覆部分請 – AntonMac

回答

0

在你的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] 
} 
+0

但我allreday有介紹thePresentation; ,請問你寫的代碼你的意思是 – AntonMac

+0

你的startDidElement代碼寫在哪裏??我的意思是在哪個階級或的viewController – vishy

+0

在我XMLParser的 – AntonMac

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

首先我有未申報的標識符應用程序的錯誤,我添加AppDelegate *應用程序;在我的頭文件,但是,我有錯誤終止應用程序由於未捕獲的異常'NSInvalidArgumentException',原因:'*** - [NSPlaceholderString initWithString:]:nil參數' – AntonMac

+0

[788:c07]演示文稿是:(null)計數是:0 [788:c07]幻燈片標籤是:(null) – AntonMac

+0

這意味着'app.presentationArray'中沒有任何內容。沒有演示文稿添加到它。 – 2012-10-15 18:17:33