根据Core Spotlight搜索词从AppDelegate显示可重新关联的视图

Jte*_*obs 6 objective-c presentmodalviewcontroller ios corespotlight

我在我的应用程序中设置了Core Spotlight.我导航到导航控制器中的特定视图控制器,该控制器根据标题显示webview网页.例如:

- (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];
}
}
Run Code Online (Sandbox Code Playgroud)

简而言之,webVC基于它的self.title和titleDetail加载特定的网页,这些值会发生变化,因为每个聚光灯唯一ID都不同.

问题不在于如果选择第一个聚光灯索引,一切正常.但是,如果我选择新的"返回%@"按钮并返回Spotlight以选择新的搜索项,我会收到以下错误:

Warning: Attempt to present <WebViewController: 0x7fced37c8be0> on <UINavigationController: 0x7fced403fa00> whose view is not in the window hierarchy!
Run Code Online (Sandbox Code Playgroud)

所以我认为我所要做的就是每次都关闭当前视图并重新加载它:

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];
}];
Run Code Online (Sandbox Code Playgroud)

不起作用,我得到以下警告/错误:

Attempting to load the view of a view controller while it is deallocating is not allowed and may result in undefined behavior
Run Code Online (Sandbox Code Playgroud)

这是有道理的.在这个特定的例子中,我如何规避这一点?

Jte*_*obs 0

我通过从导航控制器中消除它来修复它:

//Instead of this
[webVC dismissViewControllerAnimated...

//I used this
[selectedNav.presentedViewController dismissViewControllerAnimated...
Run Code Online (Sandbox Code Playgroud)