Cli*_*rum 9 nsnotifications nsnotificationcenter ios notificationcenter swift3
它只是我,还是NotificationCenter在Swift 3中成了热点?:)
我有以下设置:
// Yonder.swift
extension Notification.Name {
static let preferenceNotification = Notification.Name("preferencesChanged")
}
// I fire the notification elsewhere, like this:
NotificationCenter.default.post(name: .preferenceNotification, object: nil)
Run Code Online (Sandbox Code Playgroud)
在我的第一个视图控制器中,这很好用:
// View Controller A <-- Success!
NotificationCenter.default.addObserver(self, selector: #selector(refreshData), name: .preferenceNotification, object: nil)
func refreshData() {
// ...
}
Run Code Online (Sandbox Code Playgroud)
但是这个视图控制器:
//View Controller B <-- Crash :(
NotificationCenter.default.addObserver(self, selector: #selector(loadEntries(search:)), name: .preferenceNotification, object: nil)
func loadEntries(search:String?) {
// ...
}
Run Code Online (Sandbox Code Playgroud)
...崩溃:
[NSConcreteNotification length]:发送到实例的无法识别的选择器
据我所知,我的观察者设置正确.知道我做错了什么吗?
您的问题与您的loadEntries(search:)方法有关.这不是有效的签名.与Notification Center一起使用的选择器必须没有参数或只有一个参数.如果您有一个参数,那么该参数将是Notification对象,而不是通知名称.
你loadEntries需要:
func loadEntries(_ notification: NSNotification) {
// Optional check of the name
if notification.name == .preferenceNotification {
}
}
Run Code Online (Sandbox Code Playgroud)
选择器需要是:
#selector(loadEntries(_:)) // or #selector(loadEntries)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1450 次 |
| 最近记录: |