Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 如何访问ParentViewController的功能_Swift - Fatal编程技术网

Swift 如何访问ParentViewController的功能

Swift 如何访问ParentViewController的功能,swift,Swift,我使用父视图中的goChildViewController函数切换到ChildViewController class ParentViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() } func goChildViewController() { DispatchQueue.main.async {

我使用父视图中的goChildViewController函数切换到ChildViewController

class ParentViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    func goChildViewController() {
        DispatchQueue.main.async {
            self.navigationController?.view.layer.add(CATransition().popFromRight(), forKey: nil)
            self.navigationController?.pushViewController(ChildViewController(), animated: false)
        }
    }

    func needToAccessThisFunction() {
        // I need to call this function from ChildViewController
    }

}
我想从ChildViewController访问函数“needToAccessThisFunction”

class ChildViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        //ParentViewController.needToAccessThisFunction()
    }

}
@IBAction func btnTapped(_ sender: Any) {
    if let parent = self.parent as? ViewController { //ViewController is a parent view controller here.
        parent.methodFromChild()
    }
}
我该怎么做呢?

你可以这样做,但这不是个好主意

您的
ChildViewController
现在位于导航堆栈上,因此您可以调用类似以下内容的父函数:

class ChildViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        if let parent = self.navigationController?.viewControllers[0] as? ParentViewController {
            parent.needToAccessThisFunction()
        }
    }
}

这假定
ParentViewController
位于堆栈的底部(项
[0]

您可以为此使用代理:-

     protocol requiredMethods: class {
           func a()
           func b()
        }

        class FirstVC: UIViewController, requiredMethods {
            func a() {}
            func b() {}


        func goChildViewController() {
                DispatchQueue.main.async {

        let vc = SecondVC()
               vc.delegate = self
               self.navigationController?.view.layer.add(CATransition().popFromRight(), forKey: nil)
                    self.navigationController?.pushViewController(vc, animated: false)
                }
            }

        }

        class SecondVC: UIViewController {
            weak var delegate: requiredMethods?

            // to  call method a() self.delegate?.a())
            // to  call method b() self.delegate?.b())

        }

从子视图控制器调用父视图有多种解决方案

class ChildViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        //ParentViewController.needToAccessThisFunction()
    }

}
@IBAction func btnTapped(_ sender: Any) {
    if let parent = self.parent as? ViewController { //ViewController is a parent view controller here.
        parent.methodFromChild()
    }
}
一种是使用
委托
(我强烈建议这样做),这是一个简单的例子

还有一个,通过在子视图控制器中添加下面的代码,您可以直接调用父视图

class ChildViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        //ParentViewController.needToAccessThisFunction()
    }

}
@IBAction func btnTapped(_ sender: Any) {
    if let parent = self.parent as? ViewController { //ViewController is a parent view controller here.
        parent.methodFromChild()
    }
}
ViewController
中,您需要声明

func methodFromChild() {
    print("call from child")
}

让“孩子”子类化“家长”子类。类ChildViewController:ParentViewController{}