我使用Xcode 8和swift 3。 我有一个带按钮的viewcontroller(类用户:NSViewcontroller)。 这个按钮有一个动作:
@IBAction func btnAction(_ sender: Any) {
let popover = NSPopover()
popover.contentViewController = NSStoryboard(name: "Main", bundle: nil).instantiateController(withIdentifier: "Popover") as! NSViewController
popover.show(relativeTo: button.bunds , of: button, preferredEdge: .maxX)
}
此按钮打开另一个视图控制器作为弹出窗口。 这个popover视图控制器(Class Popover:NSViewController)执行一些操作。
现在我的问题是:如何从popover类中关闭popover viewcontroller?
答案 0 :(得分:0)
不确定上面的代码,但为了创建您的PopoverView并将其解散。以下是步骤: -
1)创建一个新的View Controller名称作为PopOverViewController.swift
2)在以下内容中实现以下方法: -
func updatePopOverViewController(_ button: UIButton?, with delegate: AnyObject?) {
guard let button = button else { return }
modalPresentationStyle = .popover
popoverPresentationController?.permittedArrowDirections = [.up]
popoverPresentationController?.backgroundColor = view.backgroundColor
popoverPresentationController?.sourceView = button
popoverPresentationController?.sourceRect = button.bounds
popoverPresentationController?.delegate = OtherViewControllerClass
}
3)在OtherViewControllerClass中实现以下代码: -
extension OtherViewControllerClass: UIPopoverPresentationControllerDelegate {
func adaptivePresentationStyle(for controller: UIPresentationController) -> UIModalPresentationStyle {
return UIModalPresentationStyle.none
}
func popoverPresentationControllerDidDismissPopover(_ popoverPresentationController: UIPopoverPresentationController) {
//your code for eg. if you want to change the tint color of our button
}
//When you tap on button it will show the popover and while tapping on yourViewcontroller view it will dismiss the popover accordingly.
@IBAction func buttonClicked(_ sender: UIButton) {
let viewController = PopOverViewController()
viewController.updatePopOverViewController(sender, with: self)
present(viewController, animated: true, completion: nil)
}
}
答案 1 :(得分:0)
好吧,我可能完全在猜测,因为我不知道你的 ViewController 是如何编写的,但这应该可以正常工作
self.presentedViewController?.dismiss(animated: true, completion: nil)
这里最基本的是你可以访问提供popover的VC,然后调用dismiss。