Display a relaodable view from AppDelegate based on Core Spotlight search term
Asked Answered
M

2

6

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?

Monney answered 27/8, 2015 at 0:44 Comment(6)
Any body know how to help?Monney
have you tried with presentedViewController to find out if there is already a view presented modally and dismiss it?Elseelset
Even if I did find if one is present and dismiss it, I still run into the same problem. It would let me load the same view will it is deallocating. So I guess the real question is, how to load the view after its reallocated or better, how to know when a deallocate is complete @ElseelsetMonney
I'm guessing the main issue you have is that you can't find which is the modally presented VC on top of your tab bar and navigation controller. when you WebViewController *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 VCElseelset
I have done that @Elseelset it's in my example in the question. I don't need to find which one it is when I know which one it isMonney
the warning/error Attempting 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. in webVC you have a new VC not the one from the first opening. To reference the webVC that is being showed on screen you could use presentedViewController then over that VC dismiss it and then present a new oneElseelset
M
0

I fixed it by dismissing it from the navigation controller:

//Instead of this
[webVC dismissViewControllerAnimated...

//I used this
[selectedNav.presentedViewController dismissViewControllerAnimated...
Monney answered 4/9, 2015 at 21:52 Comment(1)
Glad my comment helped you.Elseelset
P
0

A UINavigationController, even though it inherits from UIViewController, doesn't have a non-nil view of its own, so you can't call presentViewController:animated:completion: on top of it. I think that you're confusing presentViewController (which displays a modal, and needs to be sent to the view controller that's currently being displayed) with pushViewController:animated:, which is sent to the current view controller's navigationController.

Plascencia answered 31/8, 2015 at 3:24 Comment(1)
No I am not trying to push it. I am presenting it on purpose (as a full screen webView). Which is why I'm instantiating it so I can present it over the current navigation view controller. It works and works just fine elsewhere, I just can't get it to reload when a new spotlight index is selected because it's already loaded into memory. And the webView url is loaded based off the web view controllers titleMonney
M
0

I fixed it by dismissing it from the navigation controller:

//Instead of this
[webVC dismissViewControllerAnimated...

//I used this
[selectedNav.presentedViewController dismissViewControllerAnimated...
Monney answered 4/9, 2015 at 21:52 Comment(1)
Glad my comment helped you.Elseelset

© 2022 - 2024 — McMap. All rights reserved.