提问人:user717452 提问时间:8/17/2023 更新时间:8/24/2023 访问量:56
点击通知时推送视图控制器
Push View Controller When Notification Is Tapped
问:
我有一个 Storyboard 应用程序,其中 UINavigationController 作为其 Storyboard 入口点。它的正常主视图是一个名为“ViewController”的 ViewController。我希望在用户点击传入通知时推送名为“JustNotifications”的控制器。但是,到目前为止,什么也没发生。我错过了什么?
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler {
NSDictionary *notificationData = response.notification.request.content.userInfo;
// Extract data from the notification payload
NSString *title = notificationData[@"title"];
NSString *description = notificationData[@"description"];
// Get a reference to the navigation controller
UINavigationController *navigationController = (UINavigationController *)self.window.rootViewController;
// Find the currently visible view controller in the navigation stack
UIViewController *currentViewController = navigationController.visibleViewController;
// Perform the segue
[currentViewController performSegueWithIdentifier:@"ShowJustNotificationsSegue" sender:self];
// Call the completion handler
completionHandler();
}
答:
-1赞
user717452
8/18/2023
#1
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler {
NSDictionary *notificationData = response.notification.request.content.userInfo;
NSString *title = notificationData[@"title"];
NSString *description = notificationData[@"description"];
// Instantiate the JustNotifications view controller
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
JustNotifications *newViewController = [mainStoryboard instantiateViewControllerWithIdentifier:@"JustNotifications"];
newViewController.titleText = title;
newViewController.descriptionText = description;
// Wrap the JustNotifications view controller in a new navigation controller
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:newViewController];
// Present the navigation controller modally
UIViewController *rootViewController = UIApplication.sharedApplication.keyWindow.rootViewController;
[rootViewController presentViewController:navController animated:YES completion:nil];
completionHandler();
}
评论
0赞
HangarRash
8/19/2023
请不要发布仅代码的答案。解释问题中的代码出了什么问题,并解释您的答案如何解决问题。这为帮助他人提供了更好的答案。
评论