ios 如何在 Swift 3 中从父视图控制器中删除 ChildViewController

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45367588/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 09:21:29  来源:igfitidea点击:

How to remove the ChildViewController from Parent View Controller in Swift 3

iosxcodeswift3

提问by Noorul

I am developing an IOS Application. I have added the UIViewControllerin View Pager. I want to reinitialize it when the language is changed. Here I want to remove all child UIViewControllerfrom UIViewPagerand again back to add all UIViewControllerinto Viewpager. How can I do that?

我正在开发一个 IOS 应用程序。我已经UIViewController在 View Pager 中添加了。我想在语言改变时重新初始化它。在这里,我要删除所有的孩子UIViewControllerUIViewPager,并再次返回到所有添加UIViewController到Viewpager。我怎样才能做到这一点?

Sample Code

示例代码

let viewPager = ViewPagerController()
viewPager.options = options
viewPager.dataSource = self
viewPager.delegate = self
self.addChildViewController(viewPager)

Swift 3.1

斯威夫特 3.1

xcode 8.3.3

Xcode 8.3.3

回答by Noorul

After the long search to remove the view controllers from viewpager. I did it in the following way.

经过长时间的搜索以从 viewpager 中删除视图控制器。我是通过以下方式做到的。

 if self.childViewControllers.count > 0{
        let viewControllers:[UIViewController] = self.childViewControllers
        for viewContoller in viewControllers{
            viewContoller.willMove(toParentViewController: nil)
            viewContoller.view.removeFromSuperview()
            viewContoller.removeFromParentViewController()
        }
    }

here self is , Current UIViewController which has View Pager. I need to remove all the childview controllers from the view pager. So, i get the list of view controllers from Current UIViewController. Then i removed it from the Parent view.

这里的 self 是当前的 UIViewController,它有 View Pager。我需要从视图寻呼机中删除所有子视图控制器。所以,我从 Current UIViewController 得到了视图控制器列表。然后我从父视图中删除了它。

For swift 4.2

对于快速 4.2

 if self.children.count > 0{
        let viewControllers:[UIViewController] = self.children
        for viewContoller in viewControllers{
            viewContoller.willMove(toParent: nil)
            viewContoller.view.removeFromSuperview()
            viewContoller.removeFromParent()
        }
    }

回答by Rehan Ali

childViewControllers.forEach({ $0.willMove(toParentViewController: nil); $0.view.removeFromSuperview(); $0.removeFromParentViewController() })

childViewControllers.forEach({ $0.willMove(toParentViewController: nil); $0.view.removeFromSuperview(); $0.removeFromParentViewController() })

This answer is for swift 4.2 and above one is less than 4.2 versions.

此答案适用于 swift 4.2 及以上版本,小于 4.2 版本。

children.forEach({ $0.willMove(toParent: nil); $0.view.removeFromSuperview(); $0.removeFromParent() })

children.forEach({ $0.willMove(toParent: nil); $0.view.removeFromSuperview(); $0.removeFromParent() })

Here is the more concise version for removing all child controllers from the parent. You can use self.childViewControllers to define explicitly. As this approach, you can get rid of these extra conditions to check.

这是从父控制器中删除所有子控制器的更简洁版本。您可以使用 self.childViewControllers 来明确定义。通过这种方法,您可以摆脱这些额外的条件来检查。

回答by Suhit Patil

Swift 4.2 and above

Swift 4.2 及以上

create extensionon UIViewControllerand add removeChild()method

创建extensionUIViewController,添加removeChild()方法

extension UIViewController {

    func removeChild() {
        self.children.forEach {
            
 let forgetPasswordVc = ForgetPasswordViewController()
    self.addChild(forgetPasswordVc)
    self.view.addSubview(forgetPasswordVc.view)
    forgetPasswordVc.didMove(toParent: self)
.willMove(toParent: nil)
self.removeFromParent()
self.view.removeFromSuperview()
.view.removeFromSuperview()
self.children.forEach{
    if self.children.count > 0{
        let viewControllers:[UIViewController] = self.children
        for viewContoller in viewControllers{
            viewContoller.willMove(toParent: nil)
            viewContoller.view.removeFromSuperview()
            viewContoller.removeFromParent()
        }
    }
.willMove(toParent: nil);
//Remove child view controllers
for viewController in self.childViewControllers {
     self.remove(asChildViewController: viewController)

}

//Add child view controllers
self.add(asChildViewController: ViewController)
.view.removeFromSuperview();##代码##.removeFromParent()}
.removeFromParent() } } }

Usage:

用法:

call removeChildfrom parent UIViewController

removeChild来自父母的电话UIViewController

override func viewDidAppear(_ animated: Bool) { super.viewDidAppear(animated) self.removeChild() }

override func viewDidAppear(_ animated: Bool) { super.viewDidAppear(animated) self.removeChild() }

回答by Zouhair Sassi

To add viewController:

添加视图控制器:

##代码##

To remove (inside the ForgetPasswordViewController):

删除(在 ForgetPasswordViewController 中):

##代码##

回答by Prashant Tukadiya

Swift 4.2 , XCode 10 , ios 12 , 2018 Answer

Swift 4.2 , XCode 10 , ios 12 , 2018 答案

##代码##

Hope it is helpful to someone

希望对某人有帮助

回答by user1872384

Update for Swift 4

Swift 4更新

##代码##

回答by Nikhlesh Bagdiya

For add and remove child ViewController

用于添加和删除子 ViewController

##代码##