从iOS 8开始,Apple已经通过蓝牙LE为Midi提供了外围和中央角色选项。正如他们所建议的那样,添加Peripheral ViewController应该像包含以下代码一样简单,我已经完成了。只需单击UI按钮
即可执行此方法CABTMIDILocalPeripheralViewController *midiPeripheralController = [[CABTMIDILocalPeripheralViewController alloc] init];
[self.navigationController pushViewController:midiPeripheralController animated:YES];
执行时,将返回以下错误。如何防止此错误?
***由于未捕获的异常'NSRangeException'终止应用程序,原因:'无法删除密钥路径“parentViewController”的观察者,因为它未注册为观察者。'
答案 0 :(得分:0)
import UIKit
import CoreAudioKit
class ViewController: UIViewController {
var localPeripheralViewController:CABTMIDILocalPeripheralViewController?
override func viewDidLoad() {
super.viewDidLoad()
localPeripheralViewController = CABTMIDILocalPeripheralViewController()
}
@IBAction func local(sender: AnyObject) {
self.navigationController?.pushViewController(localPeripheralViewController!, animated: true)
}
}
答案 1 :(得分:0)
当CABTMIDILocalPeripheralViewController
要关闭时,实际上会删除预定义的密钥@"parentViewController"
,由于当时未设置该密钥,因此无法将其删除,因此抛出异常。这意味着您必须自己添加该观察者,以避免违反KVO编程。在使用
-(void)yourActionOnSomeButton:(UIButton *)sender {
// ... here is your build up code of the CABTMIDILocalPeripheralViewController
// ... but before presenting the navController you add Observer yourself,
// so it can be removed when dismissing or "done"
UIButton *button = (UIButton *)sender;
popC.sourceView = button.superview;
[popC addObserver:self forKeyPath:@"parentViewController" options:(NSKeyValueObservingOptionNew) context:nil];
[self presentViewController:navController animated:YES completion:nil];
}