如何将参数传递给 swift 中的选择器?
How to pass parameter to selector in swift?
func addObserver(_ scrollView: UIScrollView) {
NotificationCenter.default.addObserver(self, selector: #selector(XXX), name:, object:)
}
@objc func getScrollView (_ notification: Notification, useScrollView : UIScrollView) {
// Doing something for scrollView
}
我想在调用 addObserver 函数时将 scrollView 传递给 objc 函数 getScrollView。我应该如何修改这两个功能?
使用block更简单,like
private var _observer: Any! // unsubscribed automatically on deinit
func addObserver(_ scrollView: UIScrollView) {
_observer = NotificationCenter.default.addObserver(forName: name,
object: nil, queue: nil) { notification in
// ... just use scrollView here
}
}
func addObserver(_ scrollView: UIScrollView) {
NotificationCenter.default.addObserver(self, selector: #selector(XXX), name:, object:)
}
@objc func getScrollView (_ notification: Notification, useScrollView : UIScrollView) {
// Doing something for scrollView
}
我想在调用 addObserver 函数时将 scrollView 传递给 objc 函数 getScrollView。我应该如何修改这两个功能?
使用block更简单,like
private var _observer: Any! // unsubscribed automatically on deinit
func addObserver(_ scrollView: UIScrollView) {
_observer = NotificationCenter.default.addObserver(forName: name,
object: nil, queue: nil) { notification in
// ... just use scrollView here
}
}