【问题标题】:Method handler for received Notification crashes the application. :(收到通知的方法处理程序使应用程序崩溃。 :(
【发布时间】:2015-07-07 10:47:45
【问题描述】:

我正在处理外部附件框架,这是我注册通知的代码..

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)

如果有人也遇到过这个问题...请分享

【问题讨论】:

  • accessoryDidConnectNotify 改成accessoryDidConnectNotify:
  • 您的函数确实有一个参数,因此选择器名称必须是@DharmeshKheni 所写的accessoryDidConnectNotify:。这是more info

标签: ios swift ios8 nsnotificationcenter nsnotifications


【解决方案1】:

如果你的方法没有任何参数,那么你可以这样调用它:

NSNotificationCenter.defaultCenter().addObserver(self, selector: "accessoryDidConnectNotify", name: EAAccessoryDidConnectNotification, object: nil)

通过使用"accessoryDidConnectNotify"

这样您就可以像这样使用该方法:

func accessoryDidConnectNotify(){   // works fine (with no arguments)

     //Your code
} 

但是如果你的方法有参数,那么你必须这样调用它:

NSNotificationCenter.defaultCenter().addObserver(self, selector: "accessoryDidConnectNotify:", name: EAAccessoryDidConnectNotification, object: nil)

通过使用这个"accessoryDidConnectNotify:"。在这里你必须添加:

现在你可以用这种方式调用你的方法了:

func accessoryDidConnectNotify(notification: NSNotification){

    //Your code
} 

【讨论】:

    猜你喜欢
    • 2019-09-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多