2015-05-07 34 views
0

我有一個名爲LogbookFormTVCUITableViewController子類,它符合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 
} 

我怎樣才能使該功能的工作與任何類,並通過對類的協議一致性?

+0

什麼類是你的'showAircraftPicker:在'方法?如果您要將委託分配給'self',則需要確保'self'符合'UIPopoverPresentationControllerDelegate'。 – AdamPro13

+0

它不在課堂上。這是一個獨立的幫手功能。 –

+0

如果你想引用'self',你將需要在類擴展中編寫函數。你可能會希望它是一個'UIViewController'擴展。或者你是否試圖將代理設置爲傳入的「控制器」? – AdamPro13

回答

3

你可以嘗試這樣的UIViewController中創建和擴展:

extension UIViewController { 

    func showAircraftPicker(delegate: UIPopoverPresentationControllerDelegate) { 

     let storyboard = UIStoryboard(name: "Popovers", bundle: nil) 
     var aircraftModal = storyboard.instantiateViewControllerWithIdentifier("AircraftModal") as! AircraftPickerVC 
     let pc = aircraftModal.popoverPresentationController 
     pc?.sourceView = self.view 
     pc?.delegate = delegate 

     return aircraftModal 
    } 
} 
+0

這很好,謝謝! –

0

注意我怎麼也設置控制器LogbookFormTVC的類型,以便其協議符合它進來了。但是我希望這個函數可以與任何類一起工作(當然,它符合正確的協議)。

太好了。因此,傳遞類型「符合正確協議:」的對象:

func showAircraftPicker(controller: UIPopoverPresentationControllerDelegate) -> UIViewController{ 

這正是允許您執行的協議。

如果要同時符合多重限制,通用更是得心應手:

func showAircraftPicker<T: UIViewController 
    where T:UIPopoverPresentationControllerDelegate>(controller: T) -> UIViewController { 
+0

嗯......這很有趣,但是'pc?.sourceView = self.view'行不起作用,因爲'UIPopoverPresentationControllerDelegate'不是'UIViewController'的子類,因此沒有'view財產。 –

+0

更新瞭如何符合多個事物在同一時間。 –