NULL 值,而不是读取 xml 文件 (Objective-C) 中的 UILabel 和 UIButton titl



我有一个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 文件

问题就在这里

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

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

如果要

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

[str isEqual:[NSNull null]

如果该值为 iS NULL ,则从 Web 服务返回。

希望对您有所帮助!

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

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

在函数 didEndElement 中,您应该删除此行:

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

还有一些内存泄漏,你应该在添加对象后释放

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

你应该释放希望对您有所帮助..

相关内容

  • 没有找到相关文章

最新更新