Martheli
Martheli

Reputation: 981

Call a View from another ViewController Swift

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?

enter image description here

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.

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:

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.

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:

class ViewController: FormViewController {

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

    @IBOutlet weak var dimView: UIView!

Upvotes: 0

Views: 3048

Answers (1)

David Pasztor
David Pasztor

Reputation: 54775

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?

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:

class PopOverViewController: UIViewController {

    var dimView:UIView?

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

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

}

Upvotes: 1

Related Questions