Swift generic variable

前端 未结 2 509
灰色年华
灰色年华 2021-01-15 20:17

I have a protocol, and I\'d like to have a variable of type UIViewController implementing mu protocol. If I try to do something like:

var delegate:UIViewCon         


        
相关标签:
2条回答
  • 2021-01-15 20:30

    You cannot declare variable type like that in Swift.

    The easiest way to solve your problem is adding UIViewController methods/properties to BouncingMenuDelegate requirement:

    @objc protocol BouncingMenuDelegate {
        // Delegate methods
        func boudcingMenu(bouncingMenu:BouncingMenu, didSelectItemAtIndex index:Int)
    
        // required methods/properties from UIViewController 
        var view:UIView { get }
        var navigationController:UINavigationController? { get }
        func presentViewController(viewControllerToPresent: UIViewController, animated flag: Bool, completion: (() -> Void)?)
    }
    
    class BouncingMenu:UIView {
        weak var delegate:BouningMenuDelegate?
    }
    
    0 讨论(0)
  • 2021-01-15 20:45

    If the class implements a protocol you don't care about which class it actually is. This should work:

    var delegate: BouncingMenuDelegate?
    
    0 讨论(0)
提交回复
热议问题