我不熟悉 Objective-C,當用戶點擊推送通知氣泡時,需要幫助來獲取 appDelegate 中當前彈出視圖控制器的幫助。
我可以為我想要的 vc 顯示 popover,但不能關閉它。我想我沒有設定根?請幫忙。謝謝!
我的故事板流程:
[Navigation Controller][ViewControllerA] -> [ViewControllerB] -> [ViewControllerC]
我的代碼只能顯示 ViewControllerC 并且關閉按鈕不起作用(關閉到 ViewControllerB)。
我希望能夠點擊關閉按鈕將 ViewControllerC 關閉到 ViewControllerB,然后點擊 B 上的關閉按鈕轉到 A。
//AppDelegate.m
- (void)userNotificationCenter:(UNUserNotificationCenter *)center
didReceiveNotificationResponse:(UNNotificationResponse *)response
withCompletionHandler:(void(^)(void))completionHandler {
NSDictionary *userInfo = response.notification.request.content.userInfo;
if ([(userInfo[kGCMMessageIDKey]) isEqual: @"ViewControllerC"]) {
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle: nil];
ViewControllerC *vc = (ViewControllerC *)[storyboard instantiateViewControllerWithIdentifier:@"ViewControllerC"];
self.window.rootViewController = vc;
} else {
//go to another vc
}
// With swizzling disabled you must let Messaging know about the message, for Analytics
// [[FIRMessaging messaging] appDidReceiveMessage:userInfo];
// Print full message.
NSLog(@"%@", userInfo);
completionHandler();
}
//ViewControllerA.m
- (void) ViewControllerB_BackButtonPressed {
[self dismissViewControllerAnimated:YES completion:nil];
}
//ViewControllerB.m
- (void) ViewControllerC_BackButtonPressed {
[self dismissViewControllerAnimated:YES completion:nil];
}
- (IBAction)backButtonPressed:(id)sender {
[self.delegate ViewControllerB_BackButtonPressed];
}
//ViewControllerC.m
- (IBAction) backButtonPressed:(id)sender {
[self.delegate ViewControllerC_BackButtonPressed];
}
uj5u.com熱心網友回復:
如果我正確理解了您的問題,您希望整個控制器堆疊都顯示出來以回應通知。在這種情況下,您需要將它們全部初始化并包裝在導航控制器中,如下所示:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle: nil];
UIViewController *va = [storyboard instantiateViewControllerWithIdentifier:@"ViewControllerA"];
UIViewController *vb = [storyboard instantiateViewControllerWithIdentifier:@"ViewControllerB"];
UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"ViewControllerC"];
UINavigationController *navController = [[UINavigationController alloc] initWithNibName:nil bundle:nil];
navController.viewControllers = @[va, vb, vc];
window.rootViewController = navController;
uj5u.com熱心網友回復:
我最終跳過 ViewControllerB 只在 A 頂部呈現彈出框 C。
- (void)userNotificationCenter:(UNUserNotificationCenter *)center
didReceiveNotificationResponse:(UNNotificationResponse *)response
withCompletionHandler:(void(^)(void))completionHandler {
ViewControllerA *viewControllerA = [mainStoryboard instantiateViewControllerWithIdentifier:@"ViewControllerA"];
//check top view controller
UIViewController *topViewController = self.window.rootViewController;
while (true) {
if (topViewController.presentedViewController) {
topViewController = topViewController.presentedViewController;
} else if ([topViewController isKindOfClass:[UINavigationController class]]) {
UINavigationController *nav = (UINavigationController *)topViewController;
topViewController = nav.topViewController;
} else {
break;
}
}
if (topViewController != viewController) {
[topViewController dismissViewControllerAnimated:YES completion:nil];
}
if ([(userInfo[kGCMMessageIDKey]) isEqual: @"ViewControllerC"]) {
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];
UIViewController *viewControllerC = [mainStoryboard instantiateViewControllerWithIdentifier:@"ViewControllerC"];
viewControllerC.modalPresentationStyle = UIModalPresentationPopover;
viewControllerC.popoverPresentationController.permittedArrowDirections = UIPopoverArrowDirectionAny;
[self.window.rootViewController presentViewController:viewControllerC animated:YES completion:NULL];
} else {
//go to another vc
}
completionHandler();
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/347487.html
