I have a lot of view controllers that use the same two functions that show and hide popup for me. Everytime I use them, I ask myself if it wouldn't be better to put them in a global class called PopupUtils for example and set the functions as static functions.
I did it and it worked but I'm not sure if it's a good thing to do because I have to pass to my function three arguments: the parent view controller, the child view controller and the popup_container view
Since it's all passed by val, is there not a problem with memory ? or any other problem I should be aware of ?
Here is my static class called Popup Utils
class PopupUtils {
static func showPopupView(parentViewController: UIViewController, childViewController: UIViewController, popupContainer: UIView) {
parentViewController.addChild(childViewController)
popupContainer.addSubview(childViewController.view)
childViewController.view.frame = popupContainer.bounds
childViewController.view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
childViewController.didMove(toParent: parentViewController)
UIView.transition(with: popupContainer, duration: 0.2, options: .transitionCrossDissolve, animations: {
popupContainer.isHidden = false
})
}
static func removePopupView(childViewController: UIViewController, popupContainer: UIView){
// Remove pop up VC from children
childViewController.willMove(toParent: nil)
childViewController.view.removeFromSuperview()
childViewController.removeFromParent()
// Hide pop up container
popupContainer.isHidden = true
// Release language menu
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "releaseMenuSwipe"), object: nil)
}
}