场景是:在视图控制器(例如:WorldViewController
)中确认了协议(如下所示),该协议用于在内容视图控制器上弹出。
我希望它可以在视图控制器(TablePopoverContentVC
)中弹出2个不同的popover内容视图控制器(让我们说TextFieldPopoverContentVC
,WorldViewController
)。
但是通过这个协议,我只能在视图控制器中关联到一种类型。
protocol ShowPopoverProtocol: class {
associatedtype PopoverContentType: PopoverBaseContent
func showPopover(contentVc: PopoverContentType, on target: UIView, preferredWidth: CGFloat)
}
extension ShowPopoverProtocol where Self: UIViewController {
func showPopover(contentVc: PopoverContentType, on target: UIView, preferredWidth: CGFloat) {
guard let popoverPresentation = configurePresentation(from: contentVc, preferredWidth: preferredWidth) else {
return
}
popoverPresentation.sourceView = target
popoverPresentation.sourceRect = target.bounds
self.present(contentVc, animated: true) {
popoverPresentation.passthroughViews = nil
}
}
private func configurePresentation(from contentVC: PopoverContentType, preferredWidth: CGFloat) -> UIPopoverPresentationController? {
let height = contentVC.getHeight()
contentVC.preferredContentSize = CGSize(width: preferredWidth, height: height)
contentVC.modalPresentationStyle = .popover
guard let popOverPresentation = contentVC.popoverPresentationController else {
return nil
}
popOverPresentation.permittedArrowDirections = .up
return popOverPresentation
}
}
答案 0 :(得分:0)
我使用以下方法,但不确定这是最好的方法。
//input[@class='ng-pristine ng-valid ng-not-empty ng-valid-required ng-touched' and @type='text']
使用protocol ShowPopoverProtocol {
func showPopover<Content: PopoverBaseContent, Target>(contentVc:Content, on target: Target, preferredWidth: CGFloat, arrowDirection: UIPopoverArrowDirection)
}
代替generic function
。