我想使用Travis M.提供的解决方案来解决这个问题: How to use dismiss an iPhone popover in an Adaptive Storyboard
但是,我需要将以下内容翻译成目标c。
任何人都可以帮忙吗?
如果您想要的是iPad上的弹出窗口,但是iPhone上有关闭按钮的模态表,那么您可以在故事板中为弹出框创建额外的导航控制器。
在Xcode 6.3故事板中,您只需连接一个视图控制器并将segue指定为"呈现为Popover"
下面的代码应该放在视图控制器中,该控制器会切换到popover,而不是popover本身:
首先设置popover委托:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if (segue.identifier == "myPopoverSegueName") {
let vc = segue.destinationViewController
vc.popoverPresentationController?.delegate = self
return
}
}
然后添加委托扩展(在视图控制器的代码下面)并动态创建导航控制器/关闭按钮:
extension myViewController: UIPopoverPresentationControllerDelegate {
func presentationController(controller: UIPresentationController, viewControllerForAdaptivePresentationStyle style: UIModalPresentationStyle) -> UIViewController? {
let btnDone = UIBarButtonItem(title: "Done", style: .Done, target: self, action: "dismiss")
let nav = UINavigationController(rootViewController: controller.presentedViewController)
nav.topViewController.navigationItem.leftBarButtonItem = btnDone
return nav
}
}
然后添加委托扩展(在视图控制器的代码下面)并动态创建导航控制器/关闭按钮:
extension myViewController: UIPopoverPresentationControllerDelegate {
func presentationController(controller: UIPresentationController, viewControllerForAdaptivePresentationStyle style: UIModalPresentationStyle) -> UIViewController? {
let btnDone = UIBarButtonItem(title: "Done", style: .Done, target: self, action: "dismiss")
let nav = UINavigationController(rootViewController: controller.presentedViewController)
nav.topViewController.navigationItem.leftBarButtonItem = btnDone
return nav
}
}
然后你添加你的解雇功能,你应该好好去:
func dismiss() {
self.dismissViewControllerAnimated(true, completion: nil)
}
答案 0 :(得分:0)
与objective-c没有什么不同。你可以通过观察它来弄清楚发生了什么。它只是为segue做准备并实现协议的一部分。
myViewController需要声明自己符合UIAdaptivePresentationControllerDelegate。那么这就是代码。
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([[segue identifier] isEqualToString:@"myPopoverSegueName"]) {
UIViewController *viewController = segue.destinationViewController;
viewController.popoverPresentationController.delegate = self
}
}
- (UIViewController *)presentationController:(UIPresentationController *)controller
viewControllerForAdaptivePresentationStyle:(UIModalPresentationStyle)style
{
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(dismiss)];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:controller.presentedViewController];
navController.topViewController.navigationItem.leftBarButtonItem = doneButton;
}
-(void) dismiss
{
[self dismissViewControllerAnimated:YES completion:nil];
}
您无需使用扩展程序或类别。不能保证这是否与特拉维斯M.所说的相同。