Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/120.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何在UILabel中显示XML解析数据_Ios_Objective C_Xml_Xcode_Uilabel - Fatal编程技术网

Ios 如何在UILabel中显示XML解析数据

Ios 如何在UILabel中显示XML解析数据,ios,objective-c,xml,xcode,uilabel,Ios,Objective C,Xml,Xcode,Uilabel,我试图将从XML文件解析的数据从web显示到名为profilesLabel的UILabel 我试图显示标签中的所有姓氏,但由于某些原因,该值被设置为电子邮件地址。有人能帮我理解为什么我是objective-c新手吗 代码如下: -(id)loadXMLByURL:(NSString *)urlString { profile = [[NSMutableArray alloc] init]; NSURL *url = [NSURL URLWithString:urlString];

我试图将从XML文件解析的数据从web显示到名为profilesLabel的UILabel

我试图显示标签中的所有姓氏,但由于某些原因,该值被设置为电子邮件地址。有人能帮我理解为什么我是objective-c新手吗

代码如下:

-(id)loadXMLByURL:(NSString *)urlString
{
    profile = [[NSMutableArray alloc] init];
    NSURL *url = [NSURL URLWithString:urlString];
    NSData *data = [[NSData alloc] initWithContentsOfURL:url];
    parser = [[NSXMLParser alloc] initWithData:data];
    parser.delegate = self;
    [parser parse];
    return self;
}

-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
    currentNodeContent = (NSMutableString *) [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
}


-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
    if([elementName isEqualToString:@"firstname"])
    {
        currentProfile = [ViewController alloc];
        isStatus = YES;
    }

    if([elementName isEqualToString:@"lastname"])
    {
        currentProfile = [ViewController alloc];
        isStatus = YES;
    }

    if([elementName isEqualToString:@"email"])
    {
        currentProfile = [ViewController alloc];
        isStatus = YES;
    }

    if([elementName isEqualToString:@"address"])
    {
        currentProfile = [ViewController alloc];
        isStatus = YES;
    }

}

-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
    if([elementName isEqualToString:@"firstname"])
    {
        currentProfile->firstName = currentNodeContent;
        NSLog(@"%@",currentProfile->firstName);
    }

    if([elementName isEqualToString:@"lastname"])
    {
        currentProfile->lastName = currentNodeContent;
        NSLog(@"%@",currentProfile->lastName);
        last_Name.text = currentProfile->lastName;
    }

    if([elementName isEqualToString:@"email"])
    {
        currentProfile->lastName = currentNodeContent;
        NSLog(@"%@",currentProfile->lastName);
        last_Name.text = currentProfile->lastName;
    }

    if([elementName isEqualToString:@"address"])
    {
        currentProfile->lastName = currentNodeContent;
        NSLog(@"%@",currentProfile->lastName);
        last_Name.text = currentProfile->lastName;
    }


    if([elementName isEqualToString:@"profiles"])
    {
        [self->profile addObject:currentProfile];
        currentProfile = nil;
        currentNodeContent = nil;
    }
}


- (IBAction)bringFont:(id)sender
{
    //Here is a button that should show the parsed data in profilesLabel when pressed
    profilesLabel.text = currentProfile->lastName; //Shows email address for some reason
    //What I want is to show every single profile in profilesLabel underneath each other       
}

- (void)viewDidLoad
{

    [profilesScrolView setScrollEnabled:YES];
    [profilesScrolView setContentSize:CGSizeMake(680, 1200)];


    [super viewDidLoad];
    xmlParser = [[ViewController alloc] loadXMLByURL:@"http://dierenpensionlindehof.nl/XML_File.xml"];

}

谢谢你的帮助,塔恩克斯

这里的问题是:

    if([elementName isEqualToString:@"profiles"])
    {
        [self->profile addObject:currentProfile];
        currentProfile = nil;
        currentNodeContent = nil;
    }
您正在将
currentProfile
设置为
nil

然后在此处使用currentProfile:

- (IBAction)bringFont:(id)sender
{
    //Here is a button that should show the parsed data in profilesLabel when pressed
    profilesLabel.text = currentProfile->lastName;
编辑: 在澄清问题是想知道如何显示不同的数据之后

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

    ...

    // At the end of each loop add current profile to the mutable array
    [profile addObject:currentProfile];

    // Now you can set currentProfile to nil
    currentProfile = nil;
}


- (IBAction)bringFont:(id)sender
{
    // If you want to display all of this data I would recommend creating a 
    // UITableView and have each entry as a row's title. But thats another 
    // issue so to set them all into the label as you asked:

    // Loop through the array and add the strings together adding a newline each time
    NSMutableString *str = [[NSMutableString alloc] init];

    for(NSObject *obj in profile)
    {
        // Obj will need to be casted to whatever type 'currentProfile' is
        [str appendFormat:@"\n %@", obj->lastName];
    }

    // Compute what height the UILabel needs to be to display this string
    CGSize expectedSize = [str sizeWithFont:[UIFont systemFontOfSize:[UIFont systemFontSize]] constrainedToSize:CGSizeMake(320, 9999) lineBreakMode:NSLineBreakByWordWrapping];

     CGRect newFrame = CGRectMake(0, 0, expectedSize.width, expectedSize.height);
    [profilesLabel setFrame:newFrame];
    [profilesLabel setText:str];
}
编辑2:

在进一步澄清问题是您没有姓氏后,请注意您正在将此函数中的所有内容分配给lastName:

if([elementName isEqualToString:@"lastname"])
{
    currentProfile->lastName = currentNodeContent;
    NSLog(@"%@",currentProfile->lastName);
    last_Name.text = currentProfile->lastName;
}

if([elementName isEqualToString:@"email"])
{
    currentProfile->lastName = currentNodeContent;
    NSLog(@"%@",currentProfile->lastName);
    last_Name.text = currentProfile->lastName;
}

if([elementName isEqualToString:@"address"])
{
    currentProfile->lastName = currentNodeContent;
    NSLog(@"%@",currentProfile->lastName);
    last_Name.text = currentProfile->lastName;
}

在您使用的每个部分中:
currentProfile->lastName=currentNodeContent您需要在currentProfile内设置其他值

谢谢你的快速回复,那我该怎么办呢?对不起,我是目标C的新手。当您将某些内容设置为零时,您将删除附加到它的数据。仅在完成后将其设置为nil,例如在ViewDidDemouse回调等中。尝试使用[profilesLabel setText:currentProfile->lastName];相反,如果这不起作用,请在此方法中输入一个NSlog,并确认此时数据不是nil/null。如果是这样的话,您没有将数据保存在解析函数的上下文之外,谢谢,但还是运气不佳。。你可以编辑上面的代码吗?这样我就可以知道你到底在说什么了meant@Amin不,我不会在别人发现问题时编辑你的代码。再简单不过了。删除此行“currentProfile=nil;”总之,替换此行“profilesLabel.text=currentProfile->lastName;”使用以下两行的NSLog(@“是否有任何值:%@”、currentProfile->lastName);[profilesLabel setText:currentProfile->lastName];'然后告诉我NSLogcurrentProfile=[ViewController alloc]打印了什么-也许你应该在这里调用一些init方法?