【问题标题】:Load specific View after push notifications推送通知后加载特定视图
【发布时间】:2017-04-11 03:47:34
【问题描述】:

当我从推送通知打开应用程序时,我需要打开(详细视图),我试图找到答案但我找不到。

我附上了显示我需要打开的视图的图像(绿色视图)。请指导我。

【问题讨论】:

标签: ios objective-c


【解决方案1】:

您可以尝试以下方法:

在 AppDelegate 类中添加这个方法:

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
   // Open Your Details view controller directly using segue
   // You can pass details id and then fetch details and display in the view.
    SettingViewController *moreVC = [[SettingViewController alloc] initWithNibName:@"SettingViewController" bundle:nil];
    UINavigationController *navigationRootController = [[UINavigationController alloc] initWithRootViewController:moreVC];
    [[[[UIApplication sharedApplication] keyWindow] rootViewController] presentViewController:navigationRootController animated:YES completion:NULL];
}

这是另一种方法:

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

       // Register the below observer in the rootviewcontroller so its registered first and than Pass NSDictionary 
       [[NSNotificationCenter defaultCenter] postNotificationName:@"pushModalPopUp" object:userInfo];
    }

【讨论】:

  • 我正在使用目标c
【解决方案2】:

Window.rootView > A > B > C

这是一个简单的方法:

  1. 当您收到通知时,向 NSUserDefault 添加一个键(例如:“NeedShowViewC” = true),然后加载视图控制器 A
  2. 在视图控制器的 viewDidLoad 中,检查该键(“NeedShowViewC”),如果为真,则加载视图 B,依此类推。
  3. 当您完成显示视图控制器 C 时,将该键设置回 false 或远程它。

【讨论】:

    【解决方案3】:

    只是关于 Code Hunter 的第一种方法的注释: 启动一个新的 viewcontorller(你的目标“绿色”VC)+ 新的 UINavigationVC:

    SettingViewController *moreVC = [[SettingViewController alloc] initWithNibName:@"SettingViewController" bundle:nil];
    UINavigationController *navigationRootController = [[UINavigationController alloc] initWithRootViewController:moreVC];
    

    这些将创建这两个 VC 的新实例。尽管这在功能上对您来说可能不是问题,但它可能会产生内存管理问题。如果用户在关闭应用程序时已经在查看绿色详细信息视图,然后收到推送,结果打开应用程序并再次自动导航到绿色 VC,您将有 2 个绿色 VC 实例内存(您可以检查 Xcode 调试器内存映射以在实践中看到这一点)。如果您一次又一次地这样做并且 ARC 没有释放 VC 的先前实例(取决于您的 MVC 实现),您最终可能会导致内存使用量膨胀。

    此外,在没有表格视图的情况下直接显示详细信息 VC 会打乱您的“向后”导航,因为您刚刚创建的新 NavVC 不会在其 VC 堆栈中包含表格视图。

    替代方案是:

    1) 使用 didReceiveRemoteNotification() 或新的(在 iOS 10 中)UNUserNotificationCenterDelegate 获取推送通知

    2) 使用应用的根视图控制器,抓住第一个选项卡的导航控制器 -> 指向绿色详细信息视图的导航控制器。

    3) 然后回滚您的导航堆栈-> 这将确保无论用户上次打开哪个 VC/视图,您都从头开始导航并取消分配详细信息 VC(或您未来的任何 VC)在分配一个新的之前添加比 tableVC 更深的)。

    4) 然后告诉tableVC,也就是你刚才抓取的navVC的rootVC,用来显示你需要的行的详细信息。您可以通过向 tableVC 添加公共 func/API 或使用 tableVC 观察到的通知来做到这一点(如 Code Hunter 的第二种方法中所建议的那样)。

    示例(它的速度很快,但应该很容易翻译成 ObjC):

    //grab the navVC - in this example I'm assuming your tabVC is the rootVC of the app you need the 1st VC of the tabVC's array
    if let topNavController = UIApplication.shared.keyWindow?.rootViewController?.viewControllers.first as? UINavigationController {
                //grab the content VC (which is your table VC)
                if let topContentController = topNavController.viewControllers.first as? MyAppsTableView {
                    //pop the navVC stack all the way back to its root (the table)
                    topNavController.popToRootViewController(animated: false)
                    //tell your table view to show detail of a certain row
                    topContentController.pleaseShowItem(item: xyzzy)
                }
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-06-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多