0

我有一个UIViewController带有一个按钮和一个标签的页面。我在我的项目中添加了 xml 解析器并且一切正常(我在控制台中对其进行了测试),现在我想将我的 XML 文件连接到我的标签以获取 XML 文件的一个属性(用于测试)。我的问题是如何将 XML 连接到我的标签。你能给我一些实现这个的提示吗?

我是 Objective-C 的新手

这是我的代码:

我只是想看看我们如何将 xml 文件连接到标签

 - (void)viewDidLoad
 {
   //appDelegate = (testAppDelegate *)[[UIApplication sharedApplication] delegate];
   label.textColor = [UIColor greenColor];
   // label.text = aBook.title; //it not working, I don't know what should I write to get one   
   // element of xml file to this label or first element of xml file

   [super viewDidLoad];

 }

这是我的xml文件

<?xml version="1.0" encoding="UTF-8"?><Books>

<Book id="1">
    <title>
        Circumference</title>
    <author>Nicholas Nicastro</author>
    <summary>Eratosthenes and the Ancient Quest to Measure the Globe.</summary>
</Book>
<Book id="2">
    <title>Copernicus Secret</title>
    <author>Jack Repcheck</author>
    <summary>How the scientific revolution began</summary>
</Book>
<Book id="3">
    <title>Angels and Demons</title>
    <author>Dan Brown</author>
    <summary>Robert Langdon is summoned to a Swiss research facility to analyze a cryptic symbol
   seared into the chest of a murdered physicist.</summary>
</Book>
<Book id="4">
    <title>Keep the Aspidistra Flying</title>
    <author>George Orwell</author>
    <summary>A poignant and ultimately hopeful look at class and society, Keep the Aspidistra 
 Flying pays tribute to the stubborn virtues of ordinary people who keep the aspidistra
  flying.    
 </summary>
</Book>

如果这里需要它是我的解析器类:

- (Presentation1NameXML *) initXMLParser {

appDelegate = (testAppDelegate *)[[UIApplication sharedApplication] delegate];

return self;
}

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

if([elementName isEqualToString:@"Books"]) {
    //Initialize the array.
    appDelegate.books = [[NSMutableArray alloc] init];
}
else if([elementName isEqualToString:@"Book"]) {

    //Initialize the book.
    aBook = [[Presentation1Name alloc] init];

    //Extract the attribute here.
    aBook.bookID = [[attributeDict objectForKey:@"id"] integerValue];

    NSLog(@"Reading id value :%i", aBook.bookID);
}

NSLog(@"Processing Element: %@", elementName);
 }

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

if(!currentElementValue)
    currentElementValue = [[NSMutableString alloc] initWithString:string];
    else
        [currentElementValue appendString:string];

NSLog(@"Processing Value: %@", currentElementValue);

}   

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

if([elementName isEqualToString:@"Books"])
    return;

//There is nothing to do if we encounter the Books element here.
//If we encounter the Book element howevere, we want to add the book object to the array
// and release the object.
if([elementName isEqualToString:@"Book"]) {
    [appDelegate.books addObject:aBook];
    aBook = nil;
}
else
    [aBook setValue:currentElementValue forKey:elementName];
currentElementValue = nil;
}



@end

编辑:

当我使用

 NSLog(@"%@", aBook.title); I got NULL

编辑

在我看到的一些样本中,他们也有类似的东西

 Book *aBook = [appDelegate.books objectAtIndex:indexPath.row]; 

因为他们在 tableView 中使用它但是我没有任何表我应该写什么而不是 objectAtIndex:indexPath.row];

4

1 回答 1

0
- (void)viewDidLoad
 {
   appDelegate = (testAppDelegate *)[[UIApplication sharedApplication] delegate];
   Book *aBook = [appDelegate.books objectAtIndex:0]; //This will load the first book
   label.textColor = [UIColor greenColor];
   label.text = aBook.title;   
   [super viewDidLoad];

 }

如果您告诉我更多有关如何使用按钮和标签加载 viewController 的信息,我可以给您更好的答案。

于 2012-10-10T09:34:05.950 回答