我有很多视图控制器,它们使用与为我显示和隐藏弹出窗口相同的两个功能。每次使用它们时,我都会问自己是否最好将它们放在名为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)
}
}
这还不错,但是扩展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
具有两个属性popupContainer
和childViewController
,如果它们是可选更改,并相应地处理类型。
扩展中的两种方法可用于任何采用该协议的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)
}
}