2012-08-09 118 views
0

我有下面的代碼在我XMLAppDelegate.m文件:iOS:如何將數據傳遞給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; 
} 

- (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

In respone to number 2.你的數組是空的,因爲你沒有填充任何東西。 – 2012-08-09 22:51:23

+0

爲了響應數字1,你在哪裏調用handleLoadedXML? – 2012-08-09 22:52:04

+1

請注意,您不應該在您的應用代理中進行任何同步網絡呼叫。這可能會降低您的啓動速度,因爲您的應用程序被操作系統退出的風險更大,因爲啓動時間太長。將其更改爲異步請求,或在應用完成啓動後執行請求。 – runmad 2012-08-09 22:56:11

回答

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.productsapplicationDidFinishLaunching填充,你需要調用生成applicationDidFinishLaunching陣列的方法,說didFinishParsing,你可以做self.products = [self didFinishParsing];,然後它會被設置。

+0

你是什麼意思與'回報'? – filou 2012-08-09 23:07:40

+0

我在說你可以返回你需要的數組。事情是'applicationDidFinishLaunching'很早就發生了。如果你想'self.products'填充它,你需要把它設置在那裏,而不是其他方法。 – aforaudrey 2012-08-09 23:21:15

+0

謝謝你,@iBlue,但現在我收到警告:Void方法「..」不應返回值。 – filou 2012-08-10 09:51:24