gpt4 book ai didi

ios - 子类化时避免违反 LSP

转载 作者:搜寻专家 更新时间:2023-10-31 22:30:17 25 4
gpt4 key购买 nike

在 objective-C 中,我可以子类化一个 View Controller ,如下所示。

class KeyboardObserverViewController: UIViewController {

var tableView: UITableView?

init() {
super.init(nibName: nil, bundle: nil)
NotificationCenter.default.addObserver(self, selector: #selector(KeyboardObserverViewController.keyboardDidShow(_:)), name: NSNotification.Name.UIKeyboardDidShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(KeyboardObserverViewController.keyboardWillHide(_:)), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
}

required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

func keyboardDidShow(_ notification: Notification) {
let rect = ((notification as NSNotification).userInfo![UIKeyboardFrameBeginUserInfoKey] as! NSValue).cgRectValue
if let tableView = tableView {
let insets = UIEdgeInsetsMake(tableView.contentInset.top, 0, rect.height, 0)
tableView.contentInset = insets
tableView.scrollIndicatorInsets = insets
}
}

func keyboardWillHide(_ notification: Notification) {
if let tableView = tableView {
let insets = UIEdgeInsetsMake(tableView.contentInset.top, 0, 0, 0)
UIView.animate(withDuration: 0.3, animations: {
tableView.contentInset = insets
tableView.scrollIndicatorInsets = insets
})
}
}

deinit {
NotificationCenter.default.removeObserver(self)
}
}

并覆盖 TableView 变量并返回一个更专业的 TableView (即 UITableView 的子类)。然后我可以在需要时转换 TableView 变量。在 Swift 中,这有点棘手,如 this post 中所述。 .

那么你如何将这个 View Controller 子类化,创建一个具有更多特殊性的类,同时避免 LSP violation .或者子类化一个 View Controller (以及子类化它的变量)太棘手了?

编辑:关于我的帖子可能类似于 this post 的建议- 我更专注于处理代码重复而不是类与结构。

澄清:我专门在 Swift 中寻找一种方法(或最佳实践),它允许我一次编写此代码,并将其用于各种使用其 CustomTableView 实例的 View Controller 子类拥有。

最佳答案

以下情况如何:

1 一些用于获取 UITableView 子类的通用协议(protocol)。

protocol TableViewContainer {
associatedtype T : UITableView
var tableView : T? { get }
}

2 然后是观察者协议(protocol):

protocol KeyboardEventsObserver {
func registerKeyboardEvents()
func keyboardDidShow(_ notification: Notification)
func keyboardWillHide(_ notification: Notification)
}

3 然后当观察者也是一个 TableView 容器时的扩展。所以我们可以重用代码:

extension KeyboardEventsObserver where Self : TableViewContainer {

func registerKeyboardEvents() {
NotificationCenter.default.addObserver(forName: .UIKeyboardDidShow, object: nil, queue: nil) {
notification in
self.keyboardDidShow(notification)
}
NotificationCenter.default.addObserver(forName: .UIKeyboardWillHide, object: nil, queue: nil) {
notification in
self.keyboardWillHide(notification)
}
}

func keyboardDidShow(_ notification: Notification) {
let rect = ((notification as NSNotification).userInfo![UIKeyboardFrameBeginUserInfoKey] as! NSValue).cgRectValue
if let tableView = tableView {
let insets = UIEdgeInsetsMake(tableView.contentInset.top, 0, rect.height, 0)
tableView.contentInset = insets
tableView.scrollIndicatorInsets = insets
tableView.backgroundColor = UIColor.red
}
}

func keyboardWillHide(_ notification: Notification) {
if let tableView = tableView {
let insets = UIEdgeInsetsMake(tableView.contentInset.top, 0, 0, 0)
UIView.animate(withDuration: 0.3, animations: {
tableView.contentInset = insets
tableView.scrollIndicatorInsets = insets
})
tableView.backgroundColor = UIColor.green
}
}
}

4 最后,我们只是将我们需要该功能的 UIViewController 子类化。请注意,tableView 可以是 UITableView 的任何子类。

class MyCustomTableView : UITableView {

}

class SomeController : UIViewController, KeyboardEventsObserver, TableViewContainer {

@IBOutlet var tableView: MyCustomTableView?

override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
registerKeyboardEvents()
}

override func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)
NotificationCenter.default.removeObserver(self)
}
}

关于ios - 子类化时避免违反 LSP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39827332/

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