我有这个功能,我在我的应用程序中使用,创建一个全局函数会很好:
pc?.delegate = self
我遇到的问题是CustomReportVC
。由于UIPopoverPresentationControllerDelegate
符合func globalShowPicker(pickerValues:[String], field:UITextField, controller:UIViewController) -> AnyPickerVC{
//...
pc?.delegate = controller //<-- ( ! ) Type UIViewController does not conform to UIPopoverPresentationControllerDelegate
}
,因此效果很好。
但是一旦我尝试将其创建为符合此协议的类之外的全局函数,我就会收到错误:
controller
我是UIViewController
AnyObject
还是// This works fine except the pnae is on the wrong place obviously
....
Pane p = displayField.createDraggablePane(800.0, 800.0, 400.0, 300.0);
rootView.getChildren().add(p);
// Now the pane is in the right place, but it's 'stuck'
....
rootView.setCenter(designView);
Pane p = displayField.createDraggablePane(800.0, 800.0, 400.0, 300.0);
designView.getChildren().add(p);
,它不符合。有没有办法以某种方式将协议符合全局函数?
知道如何解决这个问题吗?谢谢。 :)
答案 0 :(得分:2)
使您的全局函数通用,以指定它仅适用于某些种类的UIViewControllers。在此示例中,T
可以获取任何UIViewController类型的值,该类型也符合列出的其他协议。
func globalShowPicker< T: UIViewController where
T: UIPopoverPresentationControllerDelegate,
T: UIAdaptivePresentationControllerDelegate >
(pickerValues:[String], field:UITextField, controller: T) -> AnyPickerVC
{
//...
pc?.delegate = controller
return blah
}
它确实有点长,我还没有想出缩进所有约束的最佳方法。但它确实有效。
答案 1 :(得分:0)
尝试添加一个继承自它们的新类。像这样。
class PopoverController: UIViewController, UIPopoverPresentationControllerDelegate {
}
然后,将功能切换为这样。
func globalShowPicker(pickerValues:[String], field:UITextField, controller: PopoverController) -> AnyPickerVC{
//...
pc?.delegate = controller
}
答案 2 :(得分:0)
这样的东西?
self.presentViewController(pc, animated: true, completion: nil)
顺便说一下,如果你在做Universal,你就不能像iPhone那样展示iPad UIActivityViewController。您需要按照Apple建议的设计指南在弹出窗口中显示它。
或作为示例
@IBAction func shareButtonClicked(sender: UIButton)
{
let textToShare = "Text"
if let myWebsite = NSURL(string: "http://www.example.com/")
{
let objectsToShare = [textToShare, myWebsite]
let activityVC = UIActivityViewController(activityItems: objectsToShare, applicationActivities: nil)
var nav = UINavigationController(rootViewController: activityVC)
nav.modalPresentationStyle = UIModalPresentationStyle.Popover
var popover = nav.popoverPresentationController as UIPopoverPresentationController!
activityVC.preferredContentSize = CGSizeMake(500,600)
popover.sourceView = self.view
popover.sourceRect = CGRectMake(100,100,0,0)
self.presentViewController(nav, animated: true, completion: nil)
}
}
class MyViewController : UIViewController, UIPopoverPresentationControllerDelegate {
//code here
}
答案 3 :(得分:0)
您可以将委托作为参数传递给函数,如下所示:
class CustomReportVC: UIViewController, UIAdaptivePresentationControllerDelegate, UIPopoverPresentationControllerDelegate {
class func showPicker(pickerValues:[String], field:UITextField, delegate: UIPopoverPresentationControllerDelegate) -> UIViewController {
//Set up modal
let storyboard = UIStoryboard(name: "Popovers", bundle: nil)
let modal = storyboard.instantiateViewControllerWithIdentifier("AnyPickerModal") as! UIViewController
modal.modalPresentationStyle = UIModalPresentationStyle.Popover
let pc = modal.popoverPresentationController
pc?.permittedArrowDirections = .Down
pc?.sourceView = field
pc?.sourceRect = field.bounds
modal.preferredContentSize = CGSizeMake(300,180)
pc?.delegate = delegate
//Pass in data
modal.data = pickerValues
//Set the value from within the picker controller
modal.passDataToParent = { (value) in
field.text = value
}
return modal
}
//Required for the popover
func adaptivePresentationStyleForPresentationController(controller: UIPresentationController) -> UIModalPresentationStyle {
return .None
}
}
最后你有一个符合协议的视图控制器实例,但是这样你就可以像你想要的那样拥有全局函数,并在符合协议的视图控制器中传递自我指针&#34; UIPopoverPresentationControllerDelegate&#34;:
CustomReportVC.showPicker(pickerValues:....., delegate: self)