ios – 如何正确删除SFSafariViewController作为子视图控制器?

我正在使用 this SO answer提供的技术在SFSafariViewController中预加载一些URL,如下所示:
addChildViewController(svc)
svc.didMovetoParentViewController(self)
view.addSubview(svc.view)

并且我尝试删除Safari 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

删除Safari View控制器时是否正确进行?我错过了什么吗?还是解决这个问题的任何建议?

解决方法

如果您添加子视图控制器代码如上所述,那么我认为它的顺序应该与文档有所不同.
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.

相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...