使用静态函数来处理Swift中的弹出窗口是否很糟糕?

问题描述 投票:1回答:1

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

我做到了,它奏效了,但是我不确定这是否是一件好事,因为我必须将三个参数传递给函数:父视图控制器,子视图控制器和popup_container视图

由于全部通过val传递,所以内存没有问题吗?或其他我应该知道的问题?

这是我的静态类,称为弹出实用程序

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)

    }
}
ios swift static popup
1个回答
2
投票

这还不错,但是扩展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)

    }
}
© www.soinside.com 2019 - 2024. All rights reserved.