在Swift中使用静态函数处理弹出窗口不好吗?

Is it bad to use static functions to handle popups in Swift?

我有很多视图控制器使用相同的两个功能来为我显示和隐藏弹出窗口。每次我使用它们时,我都会问自己,将它们放在一个名为 PopupUtils 的全局 class 中并将函数设置为静态函数是否会更好。

我做到了并且成功了,但我不确定这是否是一件好事,因为我必须向我的函数传递三个参数:父视图控制器、子视图控制器和 popup_container 查看

既然都是通过val传过来的,内存是不是有问题?或者我应该注意的任何其他问题?

这是我的名为 Popup Utils

的静态 class
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)

    }
}

这不是很糟糕,但是 UIViewController

的扩展怎么样
extension UIViewController {

    func showPopupView(childViewController: UIViewController, popupContainer: UIView) {
        addChild(childViewController)
        popupContainer.addSubview(childViewController.view)
        childViewController.view.frame = popupContainer.bounds
        childViewController.view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
        childViewController.didMove(toParent: self)

        UIView.transition(with: popupContainer, duration: 0.2, options: .transitionCrossDissolve, animations: {
            popupContainer.isHidden = false
        })
    }

    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)

    }
}

摆脱参数的另一种方法是协议扩展。它假定采用 UIViewController 有两个属性 popupContainerchildViewController,如果它们是可选的,则相应地更改和处理类型。

扩展中的两个方法都适用于任何采用

协议的UIViewController
protocol PopupManageable {
    var popupContainer: UIView { get }
    var childViewController: UIViewController { get }
}

extension PopupManageable where Self : UIViewController {

    func showPopupView() {
        self.addChild(childViewController)
        popupContainer.addSubview(childViewController.view)
        childViewController.view.frame = popupContainer.bounds
        childViewController.view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
        childViewController.didMove(toParent: self)

        UIView.transition(with: popupContainer, duration: 0.2, options: .transitionCrossDissolve, animations: {
            self.popupContainer.isHidden = false
        })
    }

    func removePopupView() {

        // 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)

    }
}

我认为您必须跟踪所有内容。您正在传递视图控制器并添加一个子视图控制器。如果您以后没有意识到复杂性,这可能会导致内存泄漏。每当您添加新任务时,请继续跟踪分配。在里面。