【问题标题】:iPhone: how to reload tableView and push a detailView from AppDelegate?iPhone:如何重新加载 tableView 并从 AppDelegate 推送 detailView?
【发布时间】:2011-07-04 07:44:28
【问题描述】:

我的应用是基于导航的。其中我有一个主 tableView,它显示单元格中的提要项目。单击单元格时,会创建一个详细视图,其中显示该提要项目的详细信息。我现在正在处理推送通知。单击通知中的操作按钮时,

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

被调用。如果单击通知中的操作按钮,我如何实现该方法。它应该再次解析提要,重新加载表格视图,创建最新的提要项目详细视图并将其推送到导航堆栈中。我尝试了一些代码,但没有用。这是我写的代码。

在 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 也没有重新加载。为什么会这样?提前感谢

【问题讨论】:

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


    【解决方案1】:

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

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

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

    也不确定为什么微调器不显示,尝试从viewDidAppear 调用它,只是它看到它完全有效,如果问题出在对远程通知的反应。并使用一些断点。


    根据您的评论进行编辑:

    在接口中定义

    RootViewController *controller

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

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

    didReceiveRemoteNotification 然后你可以这样做

               [controller newFeedItem];
    

    无需再次分配它,您就可以引用同一个控制器。别忘了在-(void)dealloc发布它

    【讨论】:

    • 好吧,如果我不实例化一个新的 rootViewController,我将如何再次解析提要。所有解析都发生在 rootViewController 中。是否可以在 didReceiveRemoteNotification 中编写一些代码来访问我已经拥有的 rootViewController?
    • 是的,我在程序启动时显示 rootViewController,稍后我会收到远程通知。我希望在单击通知中的操作按钮时重新加载 tableView。
    • 请查看我对如何重用控制器的回答的修改
    猜你喜欢
    • 2011-06-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多