如何在网站上加载标签加载内容? [英] How do I make a label load content located on a website?

查看:114
本文介绍了如何在网站上加载标签加载内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想制作一个标签,指明乐队是否在巡演(系统故障)

I want to make a label which indicates if a band is on tour (System of a down fyi)

因此,我想在界面中调整标签在我的服务器上给出的值。
(因此,它需要从(.html)文件中提取数据并将其显示为标签。)

So I want a label in the interface to adjust to a value given on my server. ( So it needs to extract data from a (.html) file and display it as a label. )

WebViews在我看来是凌乱的,看起来更好。

WebViews are messy in my opinion and a label looks better.

推荐答案

我建议使用AFNetworking - http://afnetworking.com/ 。它将允许您从服务器中提取数据。

I'd recommend using AFNetworking - http://afnetworking.com/. It will allow you to pull data from your server.

例如,您可以创建一个包含所需数据的XML文件。然后你可以使用NSXMLParser解析它,并对数据做任何你想要的。

For example, you could create an XML file containing the data that you need. Then you can parse it using NSXMLParser and do whatever you need with the data.

NSURL *feedURL = [[NSURL alloc] initWithString:@"http://yourserver.com/yourxmlfile.xml"];
NSURLRequest *feedRequest = [[NSURLRequest alloc] initWithURL:feedURL];
AFXMLRequestOperation *feedOperation = [AFXMLRequestOperation XMLParserRequestOperationWithRequest:feedRequest success:^(NSURLRequest *request, NSHTTPURLResponse *response, NSXMLParser *XMLParser) {
    NSLog(@"XMLRequest successful - starting parser..");
    [XMLParser setDelegate:self];
    [XMLParser parse];
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, NSXMLParser *XMLParser) {
    UIAlertView *connectionError = [[UIAlertView alloc] initWithTitle:@"Connection Error" message:error.localizedDescription delegate:nil cancelButtonTitle:@"Dismiss" otherButtonTitles:nil, nil];
    [connectionError show];
}];



XML示例:



文件,您将上传到您的服务器。我使用 AFXMLRequestOperation 中的标题 yourxmlfile.xml ,但可以调用任何你想要的。

XML Example:

This is the file you will upload to your server. I use the title yourxmlfile.xml in the AFXMLRequestOperation, but call it whatever you want.

<?xml version="1.0"?>
<band>
    <bandname>The Band</bandname>
    <bandontour>1</bandontour>
</band>



使用NSXMLParser(委派)



一个ivar(在你的.h中)保存正在解析的数据。

Using NSXMLParser (delegation)

Create an ivar (in your .h) to hold the data as it is parsed.

@property (nonatomic, retain) NSString *currentProperty;

这将临时保存元素数据,然后当解析器达到<$ (NSXMLParser *)解析器didStartElement:(code> didEndElement 。

This will temporarily hold elements data, then you need to do something with it when the parser reaches didEndElement.

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {    
    if ([elementName isEqualToString:@"bandontour"]) {
        // found the <bandontour> tag
    }    
}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
    self.currentProperty = string;
}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
    if ([elementName isEqualToString:@"bandontour"]) {
        // finished getting the data in <bandontour>

        // do something now that you've got your data retrieved
        if (self.currentProperty) int bandOnTour = self.currentProperty.intValue;
        if (bandOnTour == 1) self.yourLabel.text = @"Band is on tour!";
        else self.yourLabel.text = @"Not on tour.";
    }  
}

请参阅 NSXMLParser类参考了解更多信息。

这篇关于如何在网站上加载标签加载内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆