我的XMLAppDelegate.m文件中有以下代码:
- (void)applicationDidFinishLaunching:(UIApplication *)application {
[self.window makeKeyAndVisible];
self.products = [NSMutableArray array];
XMLViewController *viewController = [[XMLViewController alloc] init];
viewController.entries = self.products; // 2. Here my Array is EMPTY. Why?
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:productData]];
self.XMLConnection = [[NSURLConnection alloc] initWithRequest:urlRequest delegate:self];
NSAssert(self.XMLConnection != nil, @"Failure to create URL connection.");
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
}
- (void)handleLoadedXML:(NSArray *)loadedData {
[self.products addObjectsFromArray:loadedData]; // 1. here I get my Data (works fine)
XMLViewController *viewController = [[XMLViewController alloc] init];
[viewController.tableView reloadData];
}
我标记了问题。是否有可能将加载的数据(loadedData)传递给applicationDidFinishLaunching:?
提前致谢..
答案 0 :(得分:1)
你的handleLoadedXML
被叫到哪里?如果您想将其传递给applicationDidFinishLaunching
,您可以让handleLoadedXML
返回该数组,然后您可以在applicationDidFinishLaunching
中调用该方法。
修改强>
这样想:
你首先有这个:
- (void)applicationDidFinishLaunching:(UIApplication *)application {
[self.window makeKeyAndVisible];
self.products = [NSMutableArray array];
XMLViewController *viewController = [[XMLViewController alloc] init];
viewController.entries = self.products; // 2. Here my Array is EMPTY. Why?
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:productData]];
self.XMLConnection = [[NSURLConnection alloc] initWithRequest:urlRequest delegate:self];
NSAssert(self.XMLConnection != nil, @"Failure to create URL connection.");
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
}
请注意,您尚未设置self.products
。它只是分配。
申请完成启动后,您有:
// say you have something like this
- (NSArray *)didFinishParsing {
return someArray;
}
此方法在某处调用,然后调用下面的方法来设置self.products
。直到现在,您的self.products
已填充。
- (void)handleLoadedXML:(NSArray *)loadedData {
[self.products addObjectsFromArray:loadedData]; // 1. here I get my Data (works fine)
XMLViewController *viewController = [[XMLViewController alloc] init];
[viewController.tableView reloadData];
}
因此,如果您希望在self.products
中填充applicationDidFinishLaunching
,则需要调用applicationDidFinishLaunching
中生成数组的方法,比如didFinishParsing
,您可以执行此操作self.products = [self didFinishParsing];
,然后就会设置。