iPhone:如何重新加载tableView并从AppDelegate推送一个detailView?

时间:2011-02-25 13:36:10

标签: iphone objective-c uiviewcontroller ios-4.2 delegation

我的应用是基于导航的。其中我有一个主tableView,显示单元格中的饲料项目。单击单元格时,将创建一个详细视图,其中显示该订阅源项的详细信息。我正在使用推送通知。当点击通知中的操作按钮时,

(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 个答案:

答案 0 :(得分:1)

不太确定您的程序流程,但我假设您在程序启动时以及稍后收到远程通知时显示rootViewController。

在您的代码中(在didReceiveRemoteNotification中),您将实例化一个新的rootViewController,并且新的rootViewController将与屏幕上已有的不同。按照您的方法,您可能需要分配控制器一次,并在远程通知到达时保留它。

我个人建议使用本地通知并在didReceiveRemoteNotification中触发本地通知并在rootViewController中捕获它。这将确保控制器的当前活动实例响应。

也不确定为什么微调器没有显示,试试从viewDidAppear调用它,只是它看到它可以工作,如果问题是对远程通知的反应。并使用一些断点。


根据您的评论进行修改:

接口定义

中的

RootViewController *controller

在您分配控制器的实现中(例如在appDidFininshLaunching

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

didReceiveRemoteNotification中,您可以

           [controller newFeedItem];

不再重新分配它,你可以参考同一个控制器。不要忘记在-(void)dealloc

中发布它