我似乎有一个可能是新手的问题,但就是这样。如何从我的解析器类中获取我的字典,该类已经为向下钻取表构建了一个树,返回到我的 Drilldowntabledelegate 类。我的应用看起来像这样。
@implementation DrillDownAppAppDelegate
@synthesize window;
@synthesize navigationController;
@synthesize data;
-(void)StartParser
{
//NSURL *url = [[NSURL alloc] initWithString:@"http://sites.google.com/site/virtuagymtestxml/testxml/Books.xml"];
//NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithContentsOfURL:url];
//local
NSString *Path = [[NSBundle mainBundle] bundlePath];
NSString *DataPath = [ Path stringByAppendingPathComponent:@"exercises.xml"];
NSData *Data = [[NSData alloc] initWithContentsOfFile:DataPath];
NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithData:Data];
//Initialize the delegate.
XMLParser *parser = [[XMLParser alloc] initXMLParser];
//Set delegate
[xmlParser setDelegate:parser];
//Start parsing the XML file.
BOOL success = [xmlParser parse];
if(success){
NSLog(@"No Errors");
}
else
{
NSLog(@"Error Error Error!!!");
}
}
- (void)applicationDidFinishLaunching:(UIApplication *)application {
[self startParser];
NSString *Path = [[NSBundle mainBundle] bundlePath];
NSString *DataPath = [Path stringByAppendingPathComponent:@"Tree2.plist"];
NSDictionary *tempDict = [[NSDictionary alloc] initWithContentsOfFile:DataPath];
//this is what needs to happen
**NSDictionary *tempDict = dictionaryFromMyParser**
self.data = tempDict;
[tempDict release];
// Configure and show the window
[window addSubview:[navigationController view]];
[window makeKeyAndVisible];
}
我的解析器文件看起来像这样
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName
attributes:(NSDictionary *)attributeDict {
if([elementName isEqualToString:@"exercises"]) {
//init tree
Tree = [NSMutableDictionary new];
Rows = [NSMutableArray new];
[Tree setObject:Rows forKey:@"Rows"];
//Initialize the array.
... all kinds of arrays here
}
if([elementName isEqualToString:@"menu_mob"]) {
amenuMob = [[menuMob alloc]init];
}
if([elementName isEqualToString:@"menuitem"]) {
amenuItem = [[menuItem alloc] init];
amenuItem.IDE = [[attributeDict objectForKey:@"id"] integerValue];
amenuItem.text = [attributeDict objectForKey:@"text"];
NSLog(@"reading id menuitem %i", amenuItem.IDE);
NSLog(@"reading text menuitem %@", amenuItem.text);
[appDelegate.menuitems addObject:amenuItem];
}
我一直在从互联网上获得灵感和信息什么有效:
- 构建一个可以被我的应用程序读取的漂亮树(用 writetoFile 测试它)
- xmlparsing 工作(我体面的 xmlfile)
- 下钻表示例
结论:如何将我的解析器构建的(由解析器构建)Tree 获取到我的 Drilldowntableappdelegate ?
附言。如果这不起作用,我也可以在我的委托文件中构建我的解析器(我知道马虎,但我认为我的填充树会在那里工作)