0

我有一个UILabel并且UIButton在不同的页面中,我想从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;
}

我的标签代码:我得到(空)

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

我的按钮代码:我得到(空)

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

我的对象类演示链接

我的对象类幻灯片链接

我的 XML 文件

4

3 回答 3

1

问题就在这里

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

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

于 2012-10-12T08:29:19.003 回答
0

If you want to check NULL values, you can use this code

[str isEqual:[NSNull null]

This returns you if that value iS NULL from web services.

Hope this helps you!

于 2012-10-12T06:27:35.227 回答
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;
}

你应该发布希望它有帮助..

于 2012-10-12T06:42:43.290 回答