Ios 收到通知的方法处理程序使应用程序崩溃:(
我正在处理外部附件框架,这是我注册认证的代码Ios 收到通知的方法处理程序使应用程序崩溃:(,ios,swift,ios8,nsnotificationcenter,nsnotifications,Ios,Swift,Ios8,Nsnotificationcenter,Nsnotifications,我正在处理外部附件框架,这是我注册认证的代码 override func viewDidLoad() { super.viewDidLoad() EAAccessoryManager.sharedAccessoryManager().registerForLocalNotifications() NSNotificationCenter.defaultCenter().addObserver(self, selector: "accessoryDidConnectNo
override func viewDidLoad() {
super.viewDidLoad()
EAAccessoryManager.sharedAccessoryManager().registerForLocalNotifications()
NSNotificationCenter.defaultCenter().addObserver(self, selector: "accessoryDidConnectNotify", name: EAAccessoryDidConnectNotification, object: nil)
}
这是我的方法处理函数
func accessoryDidConnectNotify(notification: NSNotification){
let alert : UIAlertController = UIAlertController(title: "Alert", message: "MFi Accessory Connected", preferredStyle:UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "ok", style: UIAlertActionStyle.Default, handler: { (action) -> Void in
}))
self.presentViewController(alert, animated: true, completion: nil)
我的问题是,如果我没有在accessoryDidConnectNotify函数中提供任何参数,那么当我插入MFi附件时,应用程序可以很好地处理警报视图
(i.e) func accessoryDidConnectNotify(){ // works fine (with no arguments)
}
但是我需要在accessoryDidConnectNotify函数中使用NSNotification对象来获取附件的名称
…但如果我添加NSNotification对象,则应用程序在插入MFi附件时崩溃
(i.e) func accessoryDidConnectNotify(notification: NSNotification){
} // crashes app (with arguments)
如果有人也遇到了这个问题…请分享如果你的方法没有任何参数,那么你可以这样调用它:
NSNotificationCenter.defaultCenter().addObserver(self, selector: "accessoryDidConnectNotify", name: EAAccessoryDidConnectNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "accessoryDidConnectNotify:", name: EAAccessoryDidConnectNotification, object: nil)
func accessoryDidConnectNotify(notification: NSNotification){
//Your code
}
使用“accessoryDidConnectNotify”
因此,您可以使用以下方法:
func accessoryDidConnectNotify(){ // works fine (with no arguments)
//Your code
}
但如果您的方法有参数,则必须以这种方式调用它:
NSNotificationCenter.defaultCenter().addObserver(self, selector: "accessoryDidConnectNotify", name: EAAccessoryDidConnectNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "accessoryDidConnectNotify:", name: EAAccessoryDidConnectNotification, object: nil)
func accessoryDidConnectNotify(notification: NSNotification){
//Your code
}
通过使用此“accessoryDidConnectNotify:”
。您必须在此处添加:
现在,您可以通过以下方式使用参数调用方法:
NSNotificationCenter.defaultCenter().addObserver(self, selector: "accessoryDidConnectNotify", name: EAAccessoryDidConnectNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "accessoryDidConnectNotify:", name: EAAccessoryDidConnectNotification, object: nil)
func accessoryDidConnectNotify(notification: NSNotification){
//Your code
}
只需将
accessoryDidConnectNotify
更改为accessoryDidConnectNotify:
您的函数确实有一个参数,因此选择器名称必须是@DharmeshKheni编写的accessoryDidConnectNotify:
。