I have Core Spotlight set up in my app. I navigate to a specific view controller within a navigation controller that displays a webview webpage based on the title. So 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, the webVC loads a specific web page based on it's self.title and titleDetail, these values change because every spotlight unique ID is different.
The issue isn't if the first spotlight index is selected, everything works fine. however, if I select the new 'Back To %@' button and go back 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 all I had to do was dismiss 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 doesn't work either, I get the following warning/error:
Attempting to load the view of a view controller while it is deallocating is not allowed and may result in undefined behavior
Which makes sense. How do i circumvent this in this particular instance?
presentedViewController
to find out if there is already a view presented modally and dismiss it? – ElseelsetWebViewController *webVC = [storyboard instantiateViewControllerWithIdentifier:@"WebViewController"];
you are creating a NEW VC not pointing to the one that is already on top of the stack view. You should find out which is on top of the stack view and then dismiss it with no animation and on that on completion present with animation the new VC – ElseelsetAttempting to load the view of a view controller while it is deallocating is not allowed and may result in undefined behavior
is because you are attempting to dismiss the just created VC. inwebVC
you have a new VC not the one from the first opening. To reference the webVC that is being showed on screen you could usepresentedViewController
then over that VC dismiss it and then present a new one – Elseelset