我正在使用
this SO answer提供的技术在SFSafariViewController中预加载一些URL,如下所示:
addChildViewController(svc) svc.didMoveToParentViewController(self) view.addSubview(svc.view)
svc.willMoveToParentViewController(nil) svc.view.removeFromSuperview() svc.removeFromParentViewController()
现在我可以预加载URL并显示Safari视图没有问题.然而,在我重复这个过程(预加载/显示/删除)几次(大概30次)后,应用程序将因为某些内存问题而崩溃,因为日志显示内存级别不正常或者该应用程序在应用程序被jetsam杀死崩溃.
在崩溃之前,我看到一些关于可能的泄漏警告的日志:
<Warning>: notify name "UIKeyboardSpringBoardKeyboardShow" has been registered 20 times - this may be a leak <Warning>: notify name "com.apple.SafariViewService-com.apple.uikit.viewService.connectionRequest" has been registered 20 times - this may be a leak
解决方法
如果您添加子视图控制器代码如上所述,那么我认为它的顺序应该与文档有所不同.
addChildViewController(svc) view.addSubview(svc.view) svc.didMoveToParentViewController(self)
您应该首先添加子视图,然后调用didMoveToParentViewController.尝试这个,看看它是否有效.
Listing 5-1Adding a child view controller to a container
- (void) displayContentController: (UIViewController*) content { [self addChildViewController:content]; content.view.frame = [self
frameForContentController]; [self.view
addSubview:self.currentClientView]; [content
didMoveToParentViewController:self]; }In the preceding example,notice that you call only the
didMoveToParentViewController: method of the child. That is because
the addChildViewController: method calls the child’s
willMoveToParentViewController: method for you. The reason that you must call the didMoveToParentViewController: method yourself is that the method cannot be called until after you embed the child’s view into your container’s view hierarchy.