Leg*_*las 5 nsnotifications nsnotificationcenter ios swift ios8
我正在处理外部附件框架,这是我注册notofication的代码.
override func viewDidLoad() {
super.viewDidLoad()
EAAccessoryManager.sharedAccessoryManager().registerForLocalNotifications()
NSNotificationCenter.defaultCenter().addObserver(self, selector: "accessoryDidConnectNotify", name: EAAccessoryDidConnectNotification, object: nil)
}
Run Code Online (Sandbox Code Playgroud)
这是我的方法处理功能......
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)
Run Code Online (Sandbox Code Playgroud)
而我的问题是,如果我没有在accessoryDidConnectNotify函数内给出任何参数,当我插入MFi配件时,应用程序可以正常运行警报视图.
(i.e) func accessoryDidConnectNotify(){ // works fine (with no arguments)
}
Run Code Online (Sandbox Code Playgroud)
但我需要在我的accessoryDidConnectNotify函数中使用NSNotification对象来获取附件的名称...但是如果我添加NSNotification对象,则插入MFi附件时应用程序崩溃...
(i.e) func accessoryDidConnectNotify(notification: NSNotification){
} // crashes app (with arguments)
Run Code Online (Sandbox Code Playgroud)
如果有人也遇到了问题......请分享
如果您的方法没有任何参数,那么您可以这样调用它:
NSNotificationCenter.defaultCenter().addObserver(self, selector: "accessoryDidConnectNotify", name: EAAccessoryDidConnectNotification, object: nil)
Run Code Online (Sandbox Code Playgroud)
通过使用"accessoryDidConnectNotify"。
这样您就可以使用该方法,例如:
func accessoryDidConnectNotify(){ // works fine (with no arguments)
//Your code
}
Run Code Online (Sandbox Code Playgroud)
但如果你的方法有参数,那么你必须这样调用它:
NSNotificationCenter.defaultCenter().addObserver(self, selector: "accessoryDidConnectNotify:", name: EAAccessoryDidConnectNotification, object: nil)
Run Code Online (Sandbox Code Playgroud)
通过使用这个"accessoryDidConnectNotify:". 这里你必须添加:.
现在您可以通过以下方式调用带参数的方法:
func accessoryDidConnectNotify(notification: NSNotification){
//Your code
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
282 次 |
| 最近记录: |