在UILabels中显示变量

时间:2012-01-11 04:36:31

标签: iphone objective-c ios xml xcode

我从服务器上的XML文件中获取了一些数据,这是文件的解析:

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

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

        msgAdded = [[attributeDict objectForKey:@"added"] retain];
        msgId = [[attributeDict objectForKey:@"id"] intValue];

        msgNome        = [[NSMutableString alloc] init];
        msgName        = [[NSMutableString alloc] init];
        msgMatricula   = [[NSMutableString alloc] init];
        msgSenderCode  = [[NSMutableString alloc] init];

        inNome        = NO;
        inName        = NO;
        inMatricula   = NO;
        inSenderCode  = NO;


    }

    if ( [elementName isEqualToString:@"nome"] )       { inNome = YES;  }
    if ( [elementName isEqualToString:@"nome"] )       { inName = YES;  }
    if ( [elementName isEqualToString:@"matricula"] )  { inMatricula = YES;  }
    if ( [elementName isEqualToString:@"sendercode"] ) { inSenderCode = YES; }


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

    if ( inNome )        { [msgNome appendString:string]; }
    if ( inName )        { [msgName appendString:string]; }
    if ( inMatricula )   { [msgMatricula appendString:string]; }
    if ( inSenderCode )  { [msgSenderCode appendString:string]; }

}
-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {

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

        [messages addObject:[NSDictionary dictionaryWithObjectsAndKeys:msgAdded,@"added",msgNome,@"nome",msgMatricula,@"matricula",msgSenderCode,@"sendercode",msgName,@"name",nil]];

        [[messages reverseObjectEnumerator] allObjects];

        lastId = msgId;

        [msgAdded release];
        [msgNome release];
        [msgName release];
        [msgMatricula release];
        [msgSenderCode release];

    }

    if ( [elementName isEqualToString:@"nome"]      ) { inNome = NO;}
    if ( [elementName isEqualToString:@"name"]      ) { inName = NO;}
    if ( [elementName isEqualToString:@"matricula"]  ) { inMatricula = NO;}
    if ( [elementName isEqualToString:@"sendercode"] ) { inSenderCode = NO;}
}

正如您所注意到的,下载的数据存储在变量中(@“nome”,@“matricula”..)。但是如何在UILabels中显示值,如| label.text = @“nome”|?

1 个答案:

答案 0 :(得分:2)

看起来数据实际上存储在msgNome中。 @“nome”是一个字符串表达式,而不是实例变量。所以,你可以设置label.text = msgNome;

另外,你应该:

inNome = [elementName isEqualToString:@"nome"]; 

而不是所有额外的if语句。