2011-02-25 51 views
0

我的應用程序是基於導航的。其中我有一個主要的tableView顯示單元格中的feed項目。當單擊單元格時,會創建一個detailview,其中顯示該Feed項目的詳細信息。我現在正在使用推送通知。當點擊通知中的動作按鈕時,iPhone:如何重新加載tableView並從AppDelegate推一個detailView?

(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo: 

被調用。我如何實現該方法,如果通知中的操作按鈕被點擊。它應該再次解析Feed,重新加載tableview,創建最新的feed項目detailview並將其推送到導航堆棧中。我嘗試了一些代碼,但沒有奏效。這是我寫的代碼。

中的AppDelegate:

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo { 
       RootViewController *controller = [[RootViewController alloc] init]; 
       [controller newFeedItem]; 
    } 

在RootViewController的:

- (void)newFeedItem 
    { 
     spinner = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray]; 
spinner.frame=CGRectMake(130, 170, 50, 50); 
[self.view addSubview:spinner]; 
[spinner startAnimating]; 

[self performSelector:@selector(doStuff) withObject:nil afterDelay:0.01]; 
    } 

    -(void)doStuff 
    { 

    [[self stories] removeAllObjects]; 
    [self startParsing]; 
    [self.tableView reloadData]; 
// create detailview and push it in navigational stack 
     [spinner stopAnimating]; 
    [spinner release]; 
} 

,但活動的指標沒有出現和的tableView也沒有重裝。爲什麼會這樣呢?提前Thanx

回答

1

不太清楚您的程序流程,但我假設您在程序啓動時顯示rootViewController,並在稍後您收到遠程通知。

在你的代碼中(在didReceiveRemoteNotification),你正在實例化一個新的rootViewController,並且這個新的將與已經在屏幕上的那個不同。按照您的方法,您可能需要分配控制器一次,並在遠程通知到達時保留它。

我個人建議使用本地通知並在didReceiveRemoteNotification中觸發本地通知並在rootViewController中捕獲它。這將確保當前活動的控制器實例響應。

也不知道爲什麼微調不顯示,試圖從viewDidAppear調用它,只是它看到它可以工作,如果問題是與遠程通知的反應。並使用一些斷點。


編輯針對您的評論:

在接口

定義

RootViewController *controller

在執行你的Alloc控制器(例如在appDidFininshLaunching

if (controller == nil) controller = [[RootViewController alloc] init] 

didReceiveRemoteNotification然後你可以做

  [controller newFeedItem]; 

沒有再次分配它,你可以參考相同的控制器。不要忘記釋放它在-(void)dealloc

+0

好,如果我不會實例化一個新的rootViewController,我將再次解析飼料。所有解析都發生在rootViewController中。是否有可能在didReceiveRemoteNotification中寫入一些代碼來訪問我已有的rootViewController? – Piscean 2011-02-25 14:14:47

+0

是我在程序啓動時顯示rootViewController,稍後我收到遠程通知。並且我希望在點擊通知中的操作按鈕時重新加載tableView。 – Piscean 2011-02-25 14:18:30

+0

請查看我對如何重用控制器的回答的編輯 – Olaf 2011-02-25 14:42:53

相關問題