CDVViewController 在删除父级后仍在执行 JS

Posted

技术标签:

【中文标题】CDVViewController 在删除父级后仍在执行 JS【英文标题】:CDVViewController still executing JS after parent has been removed 【发布时间】:2012-09-13 21:13:35 【问题描述】:

我尝试将 Cordova CleaverView (CDVViewController) 插入从情节提要实例化的 UIViewController(SenchaViewController) 中,以呈现基于 javascript Sencha 的视图。

- (void)viewWillAppear:(BOOL)animated

[super viewWillAppear:animated];

if (![ self.slidingViewController.underLeftViewController isKindOfClass:[MenuViewController class]]) 
    self.slidingViewController.underLeftViewController  = [self.storyboard instantiateViewControllerWithIdentifier:@"Menu"];    
   

self.view.layer.shadowOpacity = 0.75f;
self.view.layer.shadowRadius = 10.0f;
self.view.layer.shadowColor = [UIColor blackColor].CGColor;


CDVViewController *cdvc = [CDVViewController new];

cdvc.wwwFolderName = @"www";
cdvc.startPage = @"sencha.html";
cdvc.useSplashScreen = NO;


if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) 

    cdvc.view.frame = CGRectMake(0, 0, 768, 1004);
 else 
    cdvc.view.frame = CGRectMake(0, 0, 320, 460);
 

[self.view addSubview:cdvc.view];
[self.view  bringSubviewToFront:self.menuButton];
[self.view  bringSubviewToFront:self.favButton];
cdvc = nil;

NSString *jsSetIdString = [NSString stringWithFormat:@"fetchGuid = function()return '%@';", [player valueForKey:@"playerID"]]; 
[[cdvc webView] stringByEvaluatingJavaScriptFromString:jsSetIdString];

[self.view addGestureRecognizer:self.slidingViewController.panGesture];

我的问题是在我删除了拥有 CDVViewController 的视图实例的父 ViewController(SenchaViewController) 之后,CDVViewController 没有被释放并且仍在后台运行,因为我仍然可以在控制台中看到 javascript 日志记录。我同时运行多个 CDVViewController 的原因。

这是应用添加父视图控制器的方式:

- (void)renderPlayer:(NSNotification*)notification 
SenchaViewController* newTopViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"SenchaView"];
NSDictionary *player = notification.object;

// Set active player
if( player != nil && [player valueForKey:@"playerID"] != @"" )        
    // Attach Player Dictionary to Sencha View
    newTopViewController.player = player;

    // Send it to the top
    CGRect frame = self.slidingViewController.topViewController.view.frame;
    self.slidingViewController.topViewController = newTopViewController;
    self.slidingViewController.topViewController.view.frame = frame;

这就是删除拥有 CDVController 的父视图控制器的方式

_topViewController.view removeFromSuperview];
[_topViewController willMoveToParentViewController:nil];
[_topViewController removeFromParentViewController];

我错过了什么吗?我假设 ARC 会为我释放这个

【问题讨论】:

【参考方案1】:

Cordova 有一些肮脏的方面,这就是其中之一。删除 CDVViewController 不足以将其释放。您还必须调用 [controller dispose] 方法。

【讨论】:

以上是关于CDVViewController 在删除父级后仍在执行 JS的主要内容,如果未能解决你的问题,请参考以下文章

使用 Razor Pages,在用户从下拉列表中选择父级后,如何将相关数据加载到 div?

将 .eslintrc 重新定位到父级后,ESLint 找不到配置“prettier/@typescript-eslint”

透明小部件不随其父级移动

C4D父级动,子集不动是啥原因

Jquery mouseover多次触发问题

即使在应用程序被删除后仍接收推送通知 iPhone