gpt4 book ai didi

ios - 在 Swift 中使用静态函数来处理弹出窗口是不是很糟糕?

转载 作者:行者123 更新时间:2023-12-01 19:36:32 25 4
gpt4 key购买 nike

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

我做到了,它奏效了,但我不确定这是否是一件好事,因为我必须将三个参数传递给我的函数:父 View Controller 、 subview Controller 和 popup_container View

既然都是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)

}
}

最佳答案

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

}
}

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

扩展中的两种方法可用于任何 UIViewController采用协议(protocol)
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)

}
}

关于ios - 在 Swift 中使用静态函数来处理弹出窗口是不是很糟糕?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59962668/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com