如何使标签加载内容位于网站上?

How do I make a label load content located on a website?(如何使标签加载内容位于网站上?)

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

问题描述

我想制作一个标签,表明乐队是否正在巡演(系统的向下 fyi)

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. )

在我看来,WebView 很乱,标签看起来更好.

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;

这将暂时保存元素数据,然后您需要在解析器到达 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 类参考了解更多信息.

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

本文标题为:如何使标签加载内容位于网站上?

基础教程推荐