我有一个类来管理来自和去往 iPad 外部附件的消息。在初始化中,我有以下代码:
- (id) init
{
self = [super init];
if (!self) return;
[[EAAccessoryManager sharedAccessoryManager] registerForLocalNotifications]; //we want to hear about accessories connecting and disconnecting
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(accessoryDidConnect:)
name:EAAccessoryDidConnectNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(accessoryDidDisconnect:)
name:EAAccessoryDidDisconnectNotification
object:nil];
...
}
在 dealloc 我有
- (void) dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self name:EAAccessoryDidDisconnectNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:EAAccessoryDidConnectNotification object:nil];
[[EAAccessoryManager sharedAccessoryManager] unregisterForLocalNotifications];
}
出于某种原因,当我将外部配件连接到 iPad 时,accessoryDidConnect: 会触发,然后是accessoryDidDisconnect:,然后是accessoryDidConnect:
我无法弄清楚为什么我会获得额外的连接和断开连接。有任何想法吗?