【问题标题】:How can I detect interactions with UIPickerView, when I don't own the delegate当我不拥有委托时,如何检测与 UIPickerView 的交互
【发布时间】:2015-06-18 15:38:53
【问题描述】:

背景: 我正在开发一个插件供其他应用程序开发人员使用。如果他们有 UIPickerView,我希望能够在用户与 PickerView 交互时做出反应。

这意味着我无法设置 UIPickerViewDelegate。对于 UITextFields,我可以使用 addTarget,因为它是从 UIControl 扩展而来的。那么当我不知道 UIPickerView 的上下文时,我将如何去听它的变化呢?我更喜欢用 Swift 来做。

【问题讨论】:

  • 您可以使用类别并使 onValueChanged 发送通知。谁想要遵守那个 UIPickerView 只需要观察那个通知。
  • 您能详细说明一下吗?我在 UIPickerView 中找不到 onValueChanged。鉴于它存在,我如何检测它的变化?而且我不能覆盖 didSelectItem 方法,因为这会影响它的实现。

标签: ios swift uikit


【解决方案1】:

也许您可以将UIPickerViewDelegate 设置为您的插件,并通过自定义YourPluginDelegate 委派(部分)功能。这样,您和您的用户都将能够检测到事件。而且它离你的实现多了一层抽象。

代码示例:

你的插件:

func pickerView(pickerView:UIPickerView, titleForRow 行:Int, forComponent 组件:Int) -> String!{ //在这里做你的事情 让响应 = self.pluginDelegate.plugin(self, titleForRow: row, forComponent: component) 返回响应 }

你的插件代理:

协议 YourPlugInDelegate{ func plugin(plugin:PlugIn, titleForRow row: Int, forComponent component:Int)->String! }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-01-23
    • 1970-01-01
    • 2018-10-24
    • 1970-01-01
    • 2012-06-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多