gpt4 book ai didi

ios - UIViewController 多重继承

转载 作者:搜寻专家 更新时间:2023-11-01 07:02:59 24 4
gpt4 key购买 nike

我有一个名为 BaseViewController 的类,它包含一个函数,我可以在其中向我的 VC 添加 header 并将其锚定

class BaseViewController: UIViewController {

let headerView: UIView = {
let view = UIView()
view.backgroundColor = UIColor.green
return view
}()

func addHeader() {
view.addSubview(headerView)
// then anchor it to top
}

}

我有另一个名为 ScrollViewController 的类,它向我的 VC 添加了一个 scrollViewController 并锚定了它

class ScrollViewController: UIViewController {

let scrollView: UIScrollView = {
let view = UIScrollView()
view.backgroundColor = UIColor.green
return view
}()

func addScrollView() {
view.addSubview(scrollView)
// then anchor it to top
}

}

最后,我的主类 HomeViewController 想要同时拥有一个 header 和一个 scrollView,所以我像这样从两者继承:

class HomeViewController: ScrollViewController, BaseViewController {

override viewDidLoad() {
super.viewDidLoad()

addScrollView()
addHeaderView()

let view = UIView()
//anchor view to bottom of the header
}
}

但是 swift 不允许多重继承,所以我尝试使用有效的协议(protocol)和扩展,但问题是我希望其他 View 能够锚定到标题和 scrollView,所以它不符合我的需要。

我该怎么做才能实现类似的东西

谢谢

最佳答案

Swift 不支持多重继承。但是,协议(protocol)和协议(protocol)扩展可以完成您想要的。

例子:

protocol HeaderProtocol {
func addHeaderView() -> UIView
}

extension HeaderProtocol where Self: UIViewController {
func addHeaderView() -> UIView {
let headerView = UIView()
headerView.backgroundColor = UIColor.green
view.addSubview(headerView)
// then anchor it to top

return headerView
}
}

protocol ScrollViewProtocol {
func addScrollView() -> UIView
}

extension ScrollViewProtocol where Self: UIViewController {
func addScrollView() -> UIView {
let scrollView = UIScrollView()
scrollView.backgroundColor = UIColor.green
view.addSubview(scrollView)
// then anchor it to top

return scrollView
}
}

class HomeViewController: UIViewController, ScrollViewProtocol, HeaderProtocol {
override func viewDidLoad() {
super.viewDidLoad()

let scrollView = addScrollView()
let headerView = addHeaderView()
}
}

替代方法:

protocol HeaderProtocol {
var headerView: UIView? { get set }
func addHeaderView() -> UIView
}

extension HeaderProtocol where Self: UIViewController {
func addHeaderView() -> UIView {
let headerView = UIView()
headerView.backgroundColor = UIColor.green
view.addSubview(headerView)
// then anchor it to top

return headerView
}
}

protocol ScrollViewProtocol {
var scrollView: UIView? { get set }
func addScrollView() -> UIView
}

extension ScrollViewProtocol where Self: UIViewController {
func addScrollView() -> UIView {
let scrollView = UIScrollView()
scrollView.backgroundColor = UIColor.green
view.addSubview(scrollView)
// then anchor it to top

return scrollView
}
}

class HomeViewController: UIViewController, ScrollViewProtocol, HeaderProtocol {
var scrollView: UIView?
var headerView: UIView?

override func viewDidLoad() {
super.viewDidLoad()

scrollView = addScrollView()
headerView = addHeaderView()
}
}

关于ios - UIViewController 多重继承,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50030145/

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