共享一个共同的功能:如何触发子类操作?

时间:2017-02-18 21:46:30

标签: generics swift3 override

我想制作以下特定功能(SatellitesViewController):

enter image description here

...进入更通用的函数(类型:UIViewController): enter image description here

问题:将UIViewerController转换回源UIViewController子类型,以便我可以将操作发送回调用源。

我收到编译错误' UIViewController'没有这样的功能;这是真的。但是,检查呼叫源(发送者)的类型会显示真实类型(具有该功能):

(lldb) po type(of: sender)
SwiftTech.SatellitesViewController

发件人是' UIViewController'什么时候应该是' SatellitesViewController':

enter image description here

问题:如何将发件人参数的类型强制为 true 类型与通用UIViewController?

这是典型的Swift通用场景吗?

<小时/> 解决方案尝试:

通用路线似乎不是此处的解决方案。
所以,我认为我可以向UIViewController父级添加抽象按钮操作以满足回调:

enter image description here

...然后覆盖子UIViewControllers中的函数(例如&#39; SatellitesViewController&#39;像这样:

extension SatellitesViewController {

    override func displayGrayOverview() {
        let swipeGesture = UISwipeGestureRecognizer(target: self, action: #selector(handleDownSwipe))
        swipeGesture.direction = .down
        self.grayOverview.positionOverLay(sender:self, parentView: self.view)
        self.grayOverview.addGestureRecognizer(swipeGesture)
    }
...
}

但这只会触发UIViewController的功能; 不是其子项的继承(覆盖)函数。

***UIController's Button Action ***

我更喜欢在 UIViewController中处理实现,而不是在UIViewController类本身内处理;或者根本不处理...与UIViewController的其他后代一样。

所以它归结为:假设我在这里正确的轨道,我如何强制覆盖函数()与父函数()相关?

<小时/> 这是每个请求的代码:

请求灰色叠加视图的主持人:

extension SatellitesViewController {

    func displayGrayOverview() {
        let swipeGesture = UISwipeGestureRecognizer(target: self, action: #selector(handleDownSwipe))
        swipeGesture.direction = .down
        self.grayOverview.positionNetworkSatOverLay(sender:self, parentView: self.view)
        self.grayOverview.addGestureRecognizer(swipeGesture)
    }

    // -----------------------------------------------------------------------------------------------------

    func showCode(completion:@escaping BasicCallBack) {
        if Bundle.main.url(forResource:"NetworkSatellites", withExtension: "pdf") != nil {
            let preview = QLPreviewController()
            preview.dataSource = self
            preview.currentPreviewItemIndex = 0

            self.present(preview, animated: true, completion: { 
                completion()
            })
        }
    }

    // -----------------------------------------------------------------------------------------------------
    // MARK: - Selector handlers

    func handleDownSwipe() {
        grayOverview.removeFromSuperview()
    }

    // -----------------------------------------------------------------------------------------------------
    // MARK: - Handle Swipe Gesture

    func handleButtonAction(sender:UIButton) {
        showCode() {completion in
            self.grayOverview.removeFromSuperview()
        }
    }

    // -----------------------------------------------------------------------------------------------------

    @IBAction func infoAction(_ sender: UIButton) {
       displayGrayOverview()
    }
}

以下是我希望对 UIViewController 类型的所有发件人提供的被叫函数vs: SatellitesViewController :< BR />

extension UIView {
  final func positionNetworkSatOverLay(sender:SatellitesViewController,
                                   parentView:UIView,
                                   overlayColor:UIColor = UIColor.overlayGray) {
        self.backgroundColor = overlayColor
        let screenBounds = UIScreen.main.bounds
        self.frame = screenBounds
        self.tag = 201

        var buttonTitles:NSArray?
        guard let navButtonURL = Bundle.main.url(forResource: "NetworkInfo", withExtension: "plist") else {
            return
        }

        buttonTitles = NSArray(contentsOf: navButtonURL)

        let myButton = UIButton(type: .custom)
        myButton.frame.size = CGSize(width: 100.0, height: 32.0)
        myButton.setTitle(String(describing: buttonTitles![0]), for: .normal)
        myButton.sizeToFit()

        self.addSubview(myButton)
        myButton.translatesAutoresizingMaskIntoConstraints = false
        let viewDictionary:Dictionary = ["subView": myButton]
        self.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-(x)-[subView(==w)]", options: [], metrics: ["x":200.0, "w":myButton.frame.size.width], views: viewDictionary))

        self.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|-(y)-[subView(==h)]", options: [], metrics: ["y":400.0,"h":myButton.frame.size.height], views: viewDictionary))


        myButton.addTarget(sender, action: #selector(sender.handleButtonAction), for: .touchUpInside)


        parentView.addSubview(self)
    }
    ...
}

0 个答案:

没有答案