- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
在 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/
来自 Liskov Substitution Principle - www.blackwasp.co.uk One common indication of non-compliance with
是否可以在 Windows 中使用 LSP(分层服务提供程序)更改传出数据包的目标套接字(IP 和端口)? 例如,我想将所有 HTTP 数据包重定向到代理服务器(给定 IP/端口)。这样我就可以为 W
在 objective-C 中,我可以子类化一个 View Controller ,如下所示。 class KeyboardObserverViewController: UIViewControll
提前为一个冗长的问题道歉。在这里特别感谢反馈。 . . 在我的工作中,我们做了很多关于日期范围的事情(日期周期,如果你愿意的话)。我们需要进行各种测量,比较两个日期周期之间的重叠等。我设计了一个接口(
我的处境与Steve McConnell's 非常相似在Code Complete中有提到。只有我的问题是基于 Vehicles 而 Trike 恰好是根据法律属于 Cars 类别。到目前为止,汽车只
本文实例讲述了PHP面向对象五大原则之里氏替换原则(LSP)。分享给大家供大家参考,具体如下: 替换原则由MIT计算机科学实验室的Liskov女士在1987年的OOPSLA大会上的一篇文章中提出,
当我注意到this answer时,我一直在阅读里氏替换原理。 。它有一个 Circle 和一个 ColoredCircle 类型,其中 ColoredCircle 的构造函数需要一个额外的参数; 颜
我正在寻找代码中的标志,以表明可能违反里氏替换原则。首先,我创建了一个简单的类和另一个继承它的类: public class Adder { public virtual int Add(in
我正在寻找代码中的标志,以表明可能违反里氏替换原则。首先,我创建了一个简单的类和另一个继承它的类: public class Adder { public virtual int Add(in
我有这段代码可以实现两种类型的门。一扇门有锁,另一扇没有。 Door 界面很简单: public interface Door { void open(); void close();
我有一个名为 Message 的基类,如下所示: public abstract class Message { protected int m_id; protected bool
为了使我的代码可测试,我正在尝试遵守 Liskov's substitution principle让我的 SwiftUI View 依赖于协议(protocol)而不是具体类型。这使我可以轻松地交换
我更多是出于好奇而不是真正关心它 ,但我一直想知道 JavaScript 事件系统是否违反了 Liskov substitution principle (LSP)或不。 调用 EventTarget
序言 是我太久没发了吗?昨天没人看,那么今天来点特别的~ 不仅把好看的视频全部pa下来,咱们还要实现自动评论、点赞、关注三连~ 宝,你也可以顺手给我个三连吗?给你个摸摸大~ 抓包分析流程 我写成了文档
我的 Neovim LSP 错误消息没有红色: 这是我的 vimrc 文件。我真的不知道是什么问题。 syntax on set guicursor= set relativenumber set n
LSP与协方差和逆变之间有什么关系?有什么关系吗? LSP 是协方差的一种形式吗? 最佳答案 我不会这么说Liskov's Substitution Principle是协方差的一种“形式”,就像泛型
关闭。这个问题是opinion-based .它目前不接受答案。 想改进这个问题?更新问题,以便 editing this post 可以用事实和引用来回答它. 8年前关闭。 Improve this
在emacs中,使用gopls但在我的路径上找不到go。但它在那里,我可以运行它。向社区伸出援手,看看我是否缺少任何东西。我可以毫无问题地运行基本的gin服务器。 使用文档中的config。 输出:
我正在开发 LSP,我需要知道确切的 URL 是什么。 例如,假设用户正在使用 Firefox 浏览页面。我想获取页面的完整 URL,无论是 HTTP 还是 HTTPS。我该怎么做? 我可以通过获取
有谁知道如何调试微软平台SDK提供的LSP示例?我阅读了有关此主题的内容,发现示例 dt_dll 生成了一个 DLL 文件,该文件可与已检查版本的 ws2_32.dll 一起使用。 问题是他们没有说明
我是一名优秀的程序员,十分优秀!