0

我的 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:?

提前致谢..

4

1 回答 1

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];了,然后就设置好了。

于 2012-08-09T22:51:12.870 回答