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

前端之家收集整理的这篇文章主要介绍了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.

原文链接:https://www.f2er.com/iOS/330166.html

猜你在找的iOS相关文章