我从服务器上的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;}
}
您会注意到,下载的数据存储在vars(@“ nome”,@“ matricula” ..)中。但是如何在UILabel中显示值,例如|。 label.text = @“ nome” |?
最佳答案
看起来数据实际上存储在msgNome
中。 @“ nome”是字符串表达式,而不是实例变量。因此,您将设置label.text = msgNome;。
此外,您应该只拥有:
inNome = [elementName isEqualToString:@"nome"];
而不是所有其他
if
语句。关于iphone - 在UILabels中显示变量,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8814210/