【发布时间】:2017-04-11 03:47:34
【问题描述】:
【问题讨论】:
标签: ios objective-c
【问题讨论】:
标签: ios objective-c
您可以尝试以下方法:
在 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];
}
【讨论】:
Window.rootView > A > B > C
这是一个简单的方法:
【讨论】:
只是关于 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)
}
}
【讨论】: