【问题标题】:Is it bad to use static functions to handle popups in Swift?在 Swift 中使用静态函数来处理弹出窗口是不是很糟糕?
【发布时间】:2020-01-29 07:57:01
【问题描述】:

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

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

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

这是我的名为 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)

    }
}

【问题讨论】:

  • 为什么不显示弹出窗口而不是嵌入它?
  • 我不知道还有其他解决方案。如何呈现弹出窗口?
  • stackoverflow.com/questions/34149386/… 这对于静态函数可能会有所帮助。
  • 您可以在 Storyboard 中创建新的 UIViewController,并从前一个屏幕呈现。然后选择你的 Previous ViewController -> Attribute Inspector -> Set transition Style to "cross溶解"。并将演示文稿设置为“当前内容”。
  • 如果你将 popupVC 的 modalPresentationStyle 设置为 .overFullScreen 然后呈现它之前的 VC 将是可见的,只要确保 popupVC 的背景是透明的

标签: ios swift static popup


【解决方案1】:

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

    }
}

【讨论】:

  • 看起来是个好主意,我对协议了解不多,我要测试一下 :) 谢谢你的帮助
【解决方案2】:

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

【讨论】:

  • 是的,这正是我所害怕的,我该如何解决这个问题?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-09-20
  • 1970-01-01
  • 2011-10-06
  • 2019-09-25
  • 2013-10-31
  • 2010-11-06
  • 2010-12-09
相关资源
最近更新 更多