[英]iOS: navigate to a view controller not directly connected from the app delegate
I am building an iOS app and I am facing with an issue which I am finding it a bit difficult to sort out. 我正在构建一个iOS应用程序,并且遇到了一个问题,发现它很难解决。
The structure of the app is such that I have a MainViewController which is the initial view controller rendered when my app launches. 应用程序的结构是这样的,我有一个MainViewController,它是我的应用程序启动时呈现的初始视图控制器。 I have built this in storyboard and are connected via segues. 我已在情节提要中构建了此文件,并通过segues将其连接。
MainViewController
-> UINavigationController
-> HomePageViewController
-> ContactListViewController
-> DetailsViewController
My hierarchy would appear as above. 我的层次结构将如上所示。 The flow works fine in normal cases. 在正常情况下,该流程运行良好。 Now my case is such that when I receive a push notification, I need to render the DetailsViewController directly. 现在我的情况是,当我收到推送通知时,我需要直接呈现DetailsViewController。
I know that my push notifications are handled in my app delegate's didReceiveRemoteNotification method. 我知道我的推送通知是在我的应用程序委托的didReceiveRemoteNotification方法中处理的。 So I have done, 所以我做了
def application(application, didReceiveRemoteNotification: userInfo)
NSLog('Remote notification: %@', userInfo['aps']['alert'])
push_notification_alert(userInfo)
end
private
def push_notification_alert(userInfo)
alert = UIAlertView.alloc.initWithTitle( 'Title',
message: userInfo['aps']['alert'],
delegate: self,
cancelButtonTitle: 'Reject',
otherButtonTitles: 'Accept', nil)
alert.show
end
def alertView(alertView, clickedButtonAtIndex:buttonIndex)
if(buttonIndex == alertView.cancelButtonIndex)
;
else
# I need to display the DetailsViewController controller from here.
end
end
What I am trying to do is that as soon as I receive a push notification, I display an alert to the user which asks him to reject or accept the notification. 我想做的是,一旦收到推送通知,我就会向用户显示警报,要求他拒绝或接受该通知。 If he accepts the notification, then I need to display the DetailsViewController. 如果他接受通知,那么我需要显示DetailsViewController。
Hope, you have got the issue. 希望,你有问题。 Do put in your inputs and it will of great help. 请输入您的意见,这将有很大帮助。
Once I found this somewhere in SO, and worked for me. 一旦我在SO的某个地方找到了它,并为我工作。 Try this: 尝试这个:
UIViewController *rootController = (UIViewController *)self.window.rootViewController;
UIViewController *notificationController = [rootController.storyboard instantiateViewControllerWithIdentifier:@"My Push Notification View"];
[rootController presentViewController:notificationController animated:YES completion:NULL];
Hope this helps .. :) 希望这可以帮助 .. :)
try the following methods,but 3rd method is not recommended because it instantiating new instance unnecessarily if already detailviewcontroller pushed in to the navigation stack 请尝试以下方法,但不建议使用第3种方法,因为如果已经将detailviewcontroller推入导航堆栈,则它不必要地实例化新实例
1)get visibleViewcontroller or topViewController and push 1)获取visibleViewcontroller或topViewController并推送
[navigationController.visibleViewController performSegueWithIdentifier:@"detailview" sender:self];
or
[navigationController.topViewController performSegueWithIdentifier:@"detailview" sender:self];
2)performSegue 2)performSegue
[[[navigationController viewControllers] objectAtIndex:0] performSegueWithIdentifier:@"detailview" sender:self];
3)instantiate new instance and push 3)实例化新实例并推送
NSString * storyboardName = @"MainStoryboard_iPhone";
NSString * viewControllerID = @"YourID";
UIStoryboard * storyboard = [UIStoryboard storyboardWithName:storyboardName bundle:nil];
DetailsViewController * controller = (DetailsViewController *)[storyboard instantiateViewControllerWithIdentifier:viewControllerID];
[self presentViewController:controller animated:YES completion:nil];
DetailsViewController.yourproperty = @"received push notification";
[yournavigationController pushViewController:DetailsViewController animated:YES];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.