如何在 appDelegate 中呈现弹出视图和关闭 Objective-C

How to present popover view and dismiss in appDelegate Objective-C

我不熟悉 Objective-C,需要有关当用户点击推送通知气泡时 appDelegate 中的当前弹出视图控制器的帮助。

我可以为我想要的 vc 显示弹出窗口,但不能关闭它。我想我没有设置根?请帮忙。谢谢!

我的故事板流程:

[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];
}

如果我对你的问题的理解正确,你希望整个堆栈的控制器都显示出来以响应通知。在这种情况下,您需要将它们全部初始化并包装在导航控制器中,如下所示:

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;

我最终跳过了 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();
}