applicationDidFinishLaunching

applicationDidFinishLaunching

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

提前致谢..

最佳答案

您的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];,然后将其设置。

关于ios - iOS:如何将数据传递到applicationDidFinishLaunching :?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/11893087/

10-10 01:49