Swift Nsnotificationcenter post 通知错误
Swift Nsnotificationcenter post notification error
我的 post 通知功能有问题。
在FirstViewController
中viewDidLoad
我有这句话:
NSNotificationCenter.defaultCenter().addObserver(self, selector: "ponresultado", name: "resultadobusqueda", object: nil)
之后我有函数:
func ponresultado(notification:NSNotification)
{
var oDato : oDatoSel = notification.object as oDatoSel
}
在 didDeselectRowAtIndexPath
方法中 TableViewController
类型的第二个视图控制器中,我有以下代码:
var oDato : oDatoSel = oDatoSel()
oDato.id = "1"
oDato.nombre = "test"
NSNotificationCenter.defaultCenter().postNotificationName("resultadobusqueda", object: oDato)
我收到此错误:
[App.FirstViewController ponresultado]: unrecognized selector sent to instance 0x797d2310
如果在 FirstViewController
中的 ponresultado
函数中,我会像这样退出 notification:NSNotification
参数:
func ponresultado()
{
var oDato : oDatoSel = notification.object as oDatoSel
}
我没有错误。为什么?
您需要在选择器名称后添加:
NSNotificationCenter.defaultCenter().addObserver(self, selector: "ponresultado:", name: "resultadobusqueda", object: nil)
因为您的方法被声明为接受 NSNotification 对象:
func ponresultado(notification:NSNotification)
{
var oDato : oDatoSel = notification.object as oDatoSel
}
如果您的方法将 NSNotification 作为参数,您应该在注册时将“:”添加到您的选择器中。所以你的行:
NSNotificationCenter.defaultCenter().addObserver(self, selector: "ponresultado", name: "resultadobusqueda", object: nil)
变成
NSNotificationCenter.defaultCenter().addObserver(self, selector: "ponresultado:", name: "resultadobusqueda", object: nil)
我的 post 通知功能有问题。
在FirstViewController
中viewDidLoad
我有这句话:
NSNotificationCenter.defaultCenter().addObserver(self, selector: "ponresultado", name: "resultadobusqueda", object: nil)
之后我有函数:
func ponresultado(notification:NSNotification)
{
var oDato : oDatoSel = notification.object as oDatoSel
}
在 didDeselectRowAtIndexPath
方法中 TableViewController
类型的第二个视图控制器中,我有以下代码:
var oDato : oDatoSel = oDatoSel()
oDato.id = "1"
oDato.nombre = "test"
NSNotificationCenter.defaultCenter().postNotificationName("resultadobusqueda", object: oDato)
我收到此错误:
[App.FirstViewController ponresultado]: unrecognized selector sent to instance 0x797d2310
如果在 FirstViewController
中的 ponresultado
函数中,我会像这样退出 notification:NSNotification
参数:
func ponresultado()
{
var oDato : oDatoSel = notification.object as oDatoSel
}
我没有错误。为什么?
您需要在选择器名称后添加:
NSNotificationCenter.defaultCenter().addObserver(self, selector: "ponresultado:", name: "resultadobusqueda", object: nil)
因为您的方法被声明为接受 NSNotification 对象:
func ponresultado(notification:NSNotification)
{
var oDato : oDatoSel = notification.object as oDatoSel
}
如果您的方法将 NSNotification 作为参数,您应该在注册时将“:”添加到您的选择器中。所以你的行:
NSNotificationCenter.defaultCenter().addObserver(self, selector: "ponresultado", name: "resultadobusqueda", object: nil)
变成
NSNotificationCenter.defaultCenter().addObserver(self, selector: "ponresultado:", name: "resultadobusqueda", object: nil)