从另一个ViewController Swift调用一个View

时间:2022-06-06 16:03:07

I have a ViewController that has a UIView set on top of it and a button that opens a popover to another ViewController. I want a button on the popover view controller to set the UIView to disable. How do I reference the UIView from the first view controller from a button in the second view controller?

我有一个ViewController,它上面有一个UIView设置,还有一个打开另一个ViewController的popover的按钮。我想要一个弹出窗口控制器上的按钮来设置UIView禁用。如何从第一个视图控制器中的第二个视图控制器中的按钮引用UIView?

从另一个ViewController Swift调用一个View

EDIT:

编辑:

Below is code that I use to call the popover view controller. Notice how I call dimView.isHidden = false from this first viewcontroller. I want to run dimView.isHidden = true from the popover view controller.

下面是我用来调用popover视图控制器的代码。请注意我是如何从第一个viewcontroller调用dimView.isHidden = false的。我想从popover视图控制器运行dimView.isHidden = true。

let popover = storyboard?.instantiateViewController(withIdentifier: "PopoverVC")

popover?.modalPresentationStyle = .popover
popover?.popoverPresentationController?.delegate = self as? UIPopoverPresentationControllerDelegate

popover?.popoverPresentationController?.sourceView = self.view
popover?.popoverPresentationController?.sourceRect = CGRect(x: self.view.bounds.midX, y: self.view.bounds.midY, width: 0, height: 0)

popover?.popoverPresentationController?.permittedArrowDirections = UIPopoverArrowDirection(rawValue: 0)

dimView.isHidden = false

self.present(popover!, animated: false)

EDIT 2:

编辑2:

Below is my popover view controller. Since it is not called PopoverVC. I updated the answer to include let popover = storyboard?.instantiateViewController(withIdentifier: "PopoverVC") as! PopOverViewController but still no luck.

下面是我的popover视图控制器。因为它不叫PopoverVC。我更新了包含let popover = storyboard?.instantiateViewController(withIdentifier:“PopoverVC”)的答案! PopOverViewController但仍然没有运气。

import UIKit


var parentController:UIViewController?

class PopOverViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


    @IBAction func closeButton(_ sender: Any) {
        self.dismiss(animated: false, completion: nil)  
    }

}

EDIT 3:

编辑3:

class ViewController: FormViewController {

    override func viewWillAppear(_ animated: Bool) {
         dimView.isHidden = true
    }

    @IBOutlet weak var dimView: UIView!

1 个解决方案

#1


1  

You can pass a reference to your current view controller when presenting your PopoverVC and then you can access its view from PopoverVC. Just create a property in PopoverVC that can store the reference, like var parentController:UIViewController?

您可以在呈现PopoverVC时将引用传递给当前视图控制器,然后可以从PopoverVC访问其视图。只需在PopoverVC中创建一个可以存储引用的属性,比如var parentController:UIViewController?

let popover = storyboard?.instantiateViewController(withIdentifier: "PopoverVC") as! PopoverViewController

popover?.modalPresentationStyle = .popover
popover?.popoverPresentationController?.delegate = self as? UIPopoverPresentationControllerDelegate

popover?.popoverPresentationController?.sourceView = self.view
popover?.popoverPresentationController?.sourceRect = CGRect(x: self.view.bounds.midX, y: self.view.bounds.midY, width: 0, height: 0)

popover?.popoverPresentationController?.permittedArrowDirections = UIPopoverArrowDirection(rawValue: 0)
popover?.dimView = self.dimView

dimView.isHidden = false

self.present(popover!, animated: false)

PopOverViewController:

PopOverViewController:

class PopOverViewController: UIViewController {

    var dimView:UIView?

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

    @IBAction func closeButton(_ sender: Any) {
        self.dismiss(animated: false, completion: nil)
    }

}

#1


1  

You can pass a reference to your current view controller when presenting your PopoverVC and then you can access its view from PopoverVC. Just create a property in PopoverVC that can store the reference, like var parentController:UIViewController?

您可以在呈现PopoverVC时将引用传递给当前视图控制器,然后可以从PopoverVC访问其视图。只需在PopoverVC中创建一个可以存储引用的属性,比如var parentController:UIViewController?

let popover = storyboard?.instantiateViewController(withIdentifier: "PopoverVC") as! PopoverViewController

popover?.modalPresentationStyle = .popover
popover?.popoverPresentationController?.delegate = self as? UIPopoverPresentationControllerDelegate

popover?.popoverPresentationController?.sourceView = self.view
popover?.popoverPresentationController?.sourceRect = CGRect(x: self.view.bounds.midX, y: self.view.bounds.midY, width: 0, height: 0)

popover?.popoverPresentationController?.permittedArrowDirections = UIPopoverArrowDirection(rawValue: 0)
popover?.dimView = self.dimView

dimView.isHidden = false

self.present(popover!, animated: false)

PopOverViewController:

PopOverViewController:

class PopOverViewController: UIViewController {

    var dimView:UIView?

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

    @IBAction func closeButton(_ sender: Any) {
        self.dismiss(animated: false, completion: nil)
    }

}