0

I can't figure out the proper way to call a function (with an IBOutlet variable inside of it) from another class. Basically after I click on the "Show Window" button, a custom alert view (container view) appears,

enter image description here

but when I click on the "Close" button, I want the alert view to disappear. Could someone please help me? Thanks, here's the code:

ViewController (main view):

import UIKit

class ViewController: UIViewController {

    @IBOutlet var background: UIView!
    @IBOutlet var windowView: UIView!

    @IBAction func showWindowBtn(_ sender: Any) {

        view.addSubview(windowView)
        windowView.center = view.center


        openAnimation()
    }

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

        background.alpha = 0
        windowView.alpha = 0

    }

// ----------   functions   ----------- \\

  // openAnimation
    func openAnimation () {

        UIView.animate(withDuration: 0.3) {

            self.background.alpha = 1
            self.windowView.alpha = 1
        }
    }


  // closeAnimation
    func closeAnimation () {

        UIView.animate(withDuration: 0.3) {

            self.background.alpha = 0
            self.windowView.alpha = 0
        }
    }
}

ViewController 2 (alert view):

import UIKit

class ViewController2: UIViewController {

    @IBAction func closeBtn(_ sender: Any) {

        /*

           In here, I want to access ViewController's 'closeAnimation()' function
           without getting the 'unexpectedly found nil while unwraping an optional value'
           error

        */
    }


    override func viewDidLoad() {
        super.viewDidLoad()
        // some code
    }
}
techyCode1
  • 49
  • 5

0 Answers0