如果这是一个显而易见的问题,我很抱歉,但我是初学iOS开发新手,我一直在寻找一个小时的答案而没有任何成功。
我正在开发一个需要连接蓝牙MIDI键盘的应用程序。我在Apple的网站上找到了以下文档,其中介绍了如何从应用程序管理蓝牙设备https://developer.apple.com/library/content/qa/qa1831/_index.html
我可以使用以下代码显示对话框并连接到键盘。问题是,没有办法解雇对话框。对于我在代码中看到的内容,添加了“完成”按钮,但在运行应用程序时它不会显示。
- (void)doneAction:(id)sender
{
[self dismissViewControllerAnimated:YES completion:nil];
}
- (IBAction)configureCentral:(id)sender
{
CABTMIDICentralViewController *viewController = [CABTMIDICentralViewController new];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:viewController];
// this will present a view controller as a popover in iPad and modal VC on iPhone
viewController.navigationItem.rightBarButtonItem =
[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone
target:self
action:@selector(doneAction:)];
navController.modalPresentationStyle = UIModalPresentationPopover;
UIPopoverPresentationController *popC = navController.popoverPresentationController;
popC.permittedArrowDirections = UIPopoverArrowDirectionAny;
popC.sourceRect = [sender frame];
UIButton *button = (UIButton *)sender;
popC.sourceView = button.superview;
[self presentViewController:navController animated:YES completion:nil];
}
CABTMIDICentralViewController本身作为导航栏的一些控件,具体取决于当前状态,不确定是否覆盖此类按钮
答案 0 :(得分:2)
这对我有用(对于iPhone版):
- (void)doneAction:(id)sender {
[self dismissViewControllerAnimated:NO completion:nil];
}
- (IBAction)configureCentral:(id)sender {
CABTMIDICentralViewController *viewControllerBT = [CABTMIDICentralViewController new];
UINavigationController *navControllerBT = [[UINavigationController alloc] initWithRootViewController:viewControllerBT];
UIButton * btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame = CGRectMake(screenSize.width-60, 40, 60, 40);
btn.titleLabel.font = [UIFont boldSystemFontOfSize:17.0];
[btn setTitle:@"Done" forState:UIControlStateNormal];
[btn addTarget:self action: @selector(doneAction:) forControlEvents: UIControlEventTouchDown];
[navControllerBT.view addSubview:btn];
[self presentViewController:navControllerBT animated:NO completion:nil];
}
- (IBAction)configureLocalPeripheral:(UIButton *)sender {
CABTMIDILocalPeripheralViewController *viewControllerBTA = [[CABTMIDILocalPeripheralViewController alloc] init];
UINavigationController *navControllerBTA = [[UINavigationController alloc] initWithRootViewController:viewControllerBTA];
UIButton * btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame = CGRectMake(screenSize.width-60, 40, 60, 40);
btn.titleLabel.font = [UIFont boldSystemFontOfSize:17.0];
[btn setTitle:@"Done" forState:UIControlStateNormal];
[btn addTarget:self action: @selector(doneAction:) forControlEvents: UIControlEventTouchDown];
[navControllerBTA.view addSubview:btn];
[self presentViewController:navControllerBTA animated:NO completion:nil];
}
答案 1 :(得分:0)
曾经有一个错误,视图控制器覆盖了导航栏的任何左键。如果我没记错的话,这在iOS 10中得到了修复。
确保故事板上有导航栏。这段代码依赖于self是一个带有UINavigationController的UIViewController的事实。
以下是一些适合我的代码:
- (IBAction)configureCentral:(UIButton*)sender {
centralViewController = [[CABTMIDICentralViewController alloc] init];
[self.navigationController pushViewController: centralViewController animated:YES];
}
-(IBAction)handleDone:(id)sender {
[self dismissViewControllerAnimated:YES completion:nil];
}
- (UIViewController *)presentationController:(UIPresentationController *)controller viewControllerForAdaptivePresentationStyle:(UIModalPresentationStyle)style {
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self action:@selector(handleDone:)];
UINavigationController *nav = (UINavigationController *)controller.presentedViewController;
nav.topViewController.navigationItem.leftBarButtonItem = doneButton;
return controller.presentedViewController;
}
- (UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller {
return UIModalPresentationOverFullScreen;
}
您可以使用相同的技巧来显示localPeripheralViewController
。