我正在构建一个iOS应用程序,我面临一个问题,我发现它有点难以理清。
应用程序的结构是这样的,我有一个MainViewController,它是我的应用程序启动时呈现的初始视图控制器。我在故事板中构建了这个,并通过segues连接。
MainViewController
-> UINavigationController
-> HomePageViewController
-> ContactListViewController
-> DetailsViewController
我的层次结构如上所示。在正常情况下,流程工作正常。现在我的情况是,当我收到推送通知时,我需要直接呈现DetailsViewController。
我知道我的推送通知是在我的app delegate的didReceiveRemoteNotification方法中处理的。所以我已经完成了,
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
我想要做的是,只要收到推送通知,我就会向用户显示一个警告,要求他拒绝或接受通知。如果他接受通知,那么我需要显示DetailsViewController。
希望你有问题。请投入您的意见,这将有很大的帮助。
答案 0 :(得分:1)
有一次我在SO的某处发现了这个,并为我工作。试试这个:
UIViewController *rootController = (UIViewController *)self.window.rootViewController;
UIViewController *notificationController = [rootController.storyboard instantiateViewControllerWithIdentifier:@"My Push Notification View"];
[rootController presentViewController:notificationController animated:YES completion:NULL];
希望这会有所帮助.. :)
答案 1 :(得分:0)
尝试以下方法,但不建议使用第3种方法,因为如果已经将detailviewcontroller推入导航堆栈,它会不必要地实例化新实例
1)获取visibleViewcontroller或topViewController并按
[navigationController.visibleViewController performSegueWithIdentifier:@"detailview" sender:self];
or
[navigationController.topViewController performSegueWithIdentifier:@"detailview" sender:self];
2)performSegue
[[[navigationController viewControllers] objectAtIndex:0] performSegueWithIdentifier:@"detailview" sender:self];
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];