我有一個名爲LogbookFormTVC
的UITableViewController
子類,它符合UIPopoverPresentationControllerDelegate
。在這一類我有一個創建和顯示酥料餅的功能:Swift/iOS控制器在通過函數時不保留委託
// --------------------
// LogbookFormTVC.swift
// --------------------
class LogbookFormTVC: UITableViewController, UIAdaptivePresentationControllerDelegate, UIPopoverPresentationControllerDelegate {
@IBAction func tapShowPopover(sender: AnyObject) {
//Tap to show the popover
self.presentViewController(showAircraftPicker(), animated: true, completion: nil)
}
//Build the popover
func showAircraftPicker() -> UIViewController{
//Set up modal
let storyboard = UIStoryboard(name: "Popovers", bundle: nil)
var aircraftModal = storyboard.instantiateViewControllerWithIdentifier("AircraftModal") as! AircraftPickerVC
let pc = aircraftModal.popoverPresentationController
pc?.sourceView = self.view
pc?.delegate = self
return aircraftModal
}
}
我想搬到這個showAircraftPicker()
功能,使之成爲我的應用程序可用任何地方,所以我將它移動到另一個這樣的文件:
// --------------------
// SomeWhereElse.swift
// --------------------
//This works
func showAircraftPicker(controller: LogbookFormTVC) -> UIViewController{
//Set up modal
let storyboard = UIStoryboard(name: "Popovers", bundle: nil)
var aircraftModal = storyboard.instantiateViewControllerWithIdentifier("AircraftModal") as! AircraftPickerVC
let pc = aircraftModal.popoverPresentationController
pc?.sourceView = self.view
pc?.delegate = self
return aircraftModal
}
注意我怎麼也得的controller
類型設置爲LogbookFormTVC
,以便其協議符合它進來了。但是我希望這個函數能夠與任何類(當然符合正確的協議)一起工作。
所以這樣做不起作用:
func showAircraftPicker(controller: AnyObject) -> UIViewController{
//Set up modal
let storyboard = UIStoryboard(name: "Popovers", bundle: nil)
var aircraftModal = storyboard.instantiateViewControllerWithIdentifier("AircraftModal") as! AircraftPickerVC
let pc = aircraftModal.popoverPresentationController
pc?.sourceView = self.view
pc?.delegate = self <-- !!! Type AnyObject does not conform to protocol UIPopoverPresentationControllerDelegate !!!
return aircraftModal
}
我怎樣才能使該功能的工作與任何類,並通過對類的協議一致性?
什麼類是你的'showAircraftPicker:在'方法?如果您要將委託分配給'self',則需要確保'self'符合'UIPopoverPresentationControllerDelegate'。 – AdamPro13
它不在課堂上。這是一個獨立的幫手功能。 –
如果你想引用'self',你將需要在類擴展中編寫函數。你可能會希望它是一個'UIViewController'擴展。或者你是否試圖將代理設置爲傳入的「控制器」? – AdamPro13