>背景:我正在开发一个插件供其他应用程序开发人员使用。如果他们有一个UIPickerView,我希望能够在用户与PickerView交互时做出反应。
这意味着我无法设置 UIPickerViewDelegate。对于UITextFields,我可以使用addTarget,因为它是从UIControl扩展而来的。那么,当我不知道UIPickerView的上下文时,我将如何去监听它的变化呢?我更喜欢在 Swift 中做到这一点。
也许您可以将UIPickerViewDelegate
设置为您的插件,并通过自定义YourPluginDelegate
委派(某些)函数。这样,您和您的用户都将能够检测到事件。这是远离您的实现的又一层抽象。
代码示例:
您的插件:
func pickerView(pickerView: UIPickerView, titleForRow row: Int, 对于组件组件:Int) ->字符串!{ 在这里做你的事 let response = self.pluginDelegate.plugin(self, titleForRow: row, forComponent: component) 返回响应}
您的插件委托:
协议 YourPlugInDelegate{ func plugin(plugin:PlugIn, titleForRow row: Int, forComponent component:Int)->String!}