gpt4 book ai didi

ios - 使用工厂模式时如何将委托(delegate)设置为 View Controller ?

转载 作者:行者123 更新时间:2023-11-28 12:23:49 26 4
gpt4 key购买 nike

我有一个 UIViewController,在 viewDidLoad 方法中我有一个返回 View 的工厂方法。

我试图将 UIViewController 设置为这些不同 View 的委托(delegate)。但是,每个 View 都有不同的委托(delegate),当我实际调用工厂方法时,它只返回一个 UIView。 UIView 当然不知道委托(delegate)。

我不能很好地将 delegate = ViewController 放在 ViewFactory 本身中,因为这意味着它会知道我的 ViewController,这很糟糕。我的另一个想法是,我可以将所有 View 转换为它们实际的样子,以便他们了解委托(delegate),但这让我复制了一堆代码,所以我想知道是否有人可以在这里指出我正确的方向?

View Controller

class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()

//This of course would have data to populate it, but I cannot show it here.
let data = [ViewTemplate]()

let someView = ViewFactory.getView((data?[currentCount - 1])!)

//so here is where I thought I could add
//someView.delegate
//but of course it is just a UIView that is returned so it
//doesn't know anything about a delegate

someContainerView.addSubview(someView)
}
}

struct ViewFactory {
static func getView(_ template: ViewTemplate) -> UIView {
switch template.viewType {
case .One:
let bundle = Bundle.main
let containerView = bundle.loadNibNamed("OneView", owner: self, options: nil)?.first as! OneView
containerView.label.text = template.text
return containerView
case .Two:
let bundle = Bundle.main
let containerView = bundle.loadNibNamed("TwoView", owner: self, options: nil)?.first as! TwoView
containerView.label.text = template.text
return containerView
case .Three:
let bundle = Bundle.main
let containerView = bundle.loadNibNamed("ThreeView", owner: self, options: nil)?.first as! ThreeView
containerView.label.text = template.text
return containerView
case .Four:
let bundle = Bundle.main
let containerView = bundle.loadNibNamed("FourView", owner: self, options: nil)?.first as! FourView
containerView.label.text = template.text
return containerView
case .Five:
let bundle = Bundle.main
let containerView = bundle.loadNibNamed("FiveView", owner: self, options: nil)?.first as! FiveView
containerView.label.text = template.text
return containerView
case .Six:
let bundle = Bundle.main
let containerView = bundle.loadNibNamed("SixView", owner: self, options: nil)?.first as! SixView
containerView.label.text = template.text
return containerView
}
}
}

最佳答案

您有多种选择。这是您可以使用的一种方法:

您可以使 UIView 的所有不同自定义子类都源自具有委托(delegate)属性的共同祖先。 (我们称它为 CustomView 类。)然后您可以让您的 ViewFactory 返回该类型的对象。

这是自定义 View 的基类:

class CustomView: UIView {
public weak var delegate: AnyObject?
//Put any other shared properties/methods of all CustomView objects here
}

作为 CustomView 子类的 View 可能如下所示:

class OneView: CustomView {
//Extra properties/methods for a OneView object
}

class TwoView: CustomView {
//Extra properties/methods for a TwoView object
}

最后,重构您的 ViewFactory 以返回 CustomView 类型的 View ,而不是普通的 UIView:

struct ViewFactory {
static func getView(_ template: ViewTemplate) -> CustomView {
switch template.viewType {
case .One:
let bundle = Bundle.main
let containerView = bundle.loadNibNamed("OneView", owner: self, options: nil)?.first as! OneView
containerView.label.text = template.text
return containerView
//Your other cases...
}
}

关于ios - 使用工厂模式时如何将委托(delegate)设置为 View Controller ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43986933/

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