在读取xml文件时,NULL值而不是UILabel和UIButton标题(Objective-C)

时间:2012-10-12 03:11:27

标签: objective-c ios xml ipad nsxmlparser

我在不同的网页上有UILabelUIButton,我想从xml文件中读取他们的标题, 但是我获得了NULL价值,在XML parser课程中,当我使用NSLOG时,我得到了答案,但是当我想要设置它们时,我有NULL,请你帮助我在这个实现中?

提前致谢!

我的XML解析器:

- (Presentation1NameXML *) initXMLParser {

appDelegate = (testAppDelegate *)[[UIApplication sharedApplication] delegate];

return self;
}

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


if ([elementName isEqualToString:@"Presentation1Name"]) {
    appDelegate.books = [[NSMutableArray alloc] init];
    NSLog(@"The Prices Count");
}
else if ([elementName isEqualToString:@"Presentation"]) {
    presentation = [[Presentation alloc] init];
    presentation.pLabel = [attributeDict objectForKey:@"label"];
    NSLog(@"khengggg %@",presentation.pLabel);
//    }else if ([elementName isEqualToString:@"Slides"]){
//        slides = [[Slide alloc] init];
}

if([elementName isEqualToString:@"slide"]) {

            slid = [[Slide alloc] init];
            NSLog(@"Processing Element: %@", elementName);

            slid.index = [[attributeDict objectForKey:@"index"] integerValue];
           NSLog(@"Reading index value :%i", slid.index);


            slid.sLabel = [attributeDict objectForKey:@"label"];
            NSLog(@"Reading label value :%@", slid.sLabel);

            slid.identifier = [attributeDict objectForKey:@"identifier"];
        NSLog(@"Reading identifier value :%@", slid.identifier);

       }
      NSLog(@"Processing Element: %@", elementName);
    }


- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {

if(!currentElementValue)
    currentElementValue = [[NSMutableString alloc] initWithString:string];
else
    [currentElementValue appendString:string];

NSLog(@"Processing Value: %@", currentElementValue);

}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName
 namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
if([elementName isEqualToString:@"Presentation1Name"]){
    return;

}

if([elementName isEqualToString:@"Presentation"]) {
    [appDelegate.books addObject:presentation];
    presentation = nil;

}

//    if ([elementName isEqualToString:@"Slides"]){
//        [appDelegate.books addObject:slides];
//        slides = nil;
//        return;
//    }

if ([elementName isEqualToString:@"slide"]){
    [appDelegate.books addObject:slid];
    slid = nil;

}else
    [presentation setValue:currentElementValue forKey:@"pLabel"];
    //[presentation setValue:currentElementValue forKey:elementName];
currentElementValue = nil;
}

我的标签代码:我得到了(null)

appDelegate = (testAppDelegate *)[[UIApplication sharedApplication] delegate];
label.textColor = [UIColor greenColor];
Presentation *p1 = [appDelegate.books objectAtIndex:0];
NSLog(@"aaaaaaaaa aaaa %@", p1);
label.text = p1.pLabel;

我的按钮代码:我得到了(null)

 UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    Slide *s1 = [appDelegate.books objectAtIndex:0];
    NSLog(@" test %@", s1.sLabel);

我的对象类演示文稿link

我的对象类幻灯片link

My XML file

3 个答案:

答案 0 :(得分:1)

问题出在这里

if ([elementName isEqualToString:@"Presentation1Name"]) {
    appDelegate.books = [[NSMutableArray alloc] init];
    NSLog(@"The Prices Count");
}

没有这样的标签(Presentation1Name),永远不会创建数组。

答案 1 :(得分:0)

如果要检查NULL值,可以使用此代码

[str isEqual:[NSNull null]

如果Web服务中的值为NULL,则返回此值。

希望这能帮到你!

答案 2 :(得分:0)

在函数didStartElement中修改你的代码,没有这个标签'Presentation1Name'

if ([elementName isEqualToString:@"Presentation"]) {
    appDelegate.books = [[NSMutableArray alloc] init];
    NSLog(@"The Prices Count");
}

和在函数didEndElement中你应该删除这一行:

[presentation setValue:currentElementValue forKey:@"pLabel"];

还有一些内存泄漏,你应该在addObject

之后发布
if([elementName isEqualToString:@"Presentation"]) {
    [appDelegate.books addObject:presentation];
    [presentation release];
    presentation = nil;

}
if ([elementName isEqualToString:@"slide"]){
    [appDelegate.books addObject:slid];
    [slid release];
    slid = nil;
}
你应该释放 希望它有用..