Display view from AppDelegate based on Core Spotlight search query

I have a Core Spotlight configured in my application. I move on to a specific view controller in a navigation controller that displays a webview webpage based on the title. For example:

- (BOOL)application:(UIApplication *)application continueUserActivity:(NSUserActivity *)userActivity restorationHandler:(void(^)(NSArray *restorableObjects))restorationHandler { if ([[userActivity activityType] isEqualToString:CSSearchableItemActionType]) { // This activity represents an item indexed using Core Spotlight, so restore the context related to the unique identifier. // The unique identifier of the Core Spotlight item is set in the activity's userInfo for the key CSSearchableItemActivityIdentifier. NSString *uniqueIdentifier = [userActivity.userInfo objectForKey:CSSearchableItemActivityIdentifier]; NSString *valueForUID = [self valueForKey:uniqueIdentifier]; self.tabBar = (UITabBarController *)self.window.rootViewController; self.tabBar.selectedIndex = 0; UINavigationController *selectedNav=(UINavigationController *)self.tabBar.selectedViewController; UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil]; WebViewController *webVCVC = [storyboard instantiateViewControllerWithIdentifier:@"WebViewController"]; webVC.title = uniqueIdentifier; webVC.titleDetail = valueForUID; [selectedNav presentViewController:webVC animated:YES completion:nil]; } } 

In short, webVC loads a specific web page based on its self.title and titleDetail, these values ​​change because each unique spotlight identifier is different.

The problem is not that the first searchlight index is selected, everything works fine. however , if I select the new "Back to% @" button and return to Spotlight to select a new search item, I get the following error:

 Warning: Attempt to present <WebViewController: 0x7fced37c8be0> on <UINavigationController: 0x7fced403fa00> whose view is not in the window hierarchy! 

So, I thought that all I had to do was discard the current view every time and reload it:

 self.tabBar = (UITabBarController *)self.window.rootViewController; self.tabBar.selectedIndex = 0; UINavigationController *selectedNav=(UINavigationController *)self.tabBar.selectedViewController; UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil]; WebViewController *webVC = [storyboard instantiateViewControllerWithIdentifier:@"WebViewController"]; [webVC dismissViewControllerAnimated:NO completion:^{ webVC.title = pubNum; webVC.titleString = pubTitle; [selectedNav presentViewController:webVC animated:YES completion:nil]; }]; 

This does not work either, the following warning / error appears:

 Attempting to load the view of a view controller while it is deallocating is not allowed and may result in undefined behavior 

It makes sense. How to get around this in this particular case?

+6
source share
2 answers

I fixed this by rejecting it from the navigation controller:

 //Instead of this [webVC dismissViewControllerAnimated... //I used this [selectedNav.presentedViewController dismissViewControllerAnimated... 
0
source

A UINavigationController , although it inherits from a UIViewController , does not have its own nil view , so you cannot call presentViewController:animated:completion: on top of it. I think that you are misleading the presentViewController (which displays modal text and should be sent to the view controller that is currently being displayed) using pushViewController:animated: which is sent to the current navigationController view controller.

0
source

All Articles