swift - 如何在 swift 中使用其他 class() 委托(delegate)和数据源函数?

标签 swift xcode swift2 picker didselectrowatindexpath

我有一个 UITextField 类,它向以下代表确认。我将此类用作全局类(它是一个小型自定义库,我将其用作微调器)。

    //MARK: PickerView Delegate
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
    return pickerDataArray![row]
}

func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
    self.text = pickerDataArray![row]
}

//MARK: TextField Delegate
func textFieldDidBeginEditing(textField: UITextField) {
    let row =  self.pickerView.selectedRowInComponent(0)
    self.text = self.pickerDataArray![row]
}

现在,我想获取选定的行,在这个类中将是 didSelectRow() 函数。那么,我如何才能在我实现它的那些类中以某种方式使用这些函数呢?

最佳答案

您可以使用创建基类并在 ChildClass 中扩展或任何您想使用的方式来实现

例子:

SuperBaseViewController 类

class SuperBaseViewController: UIPickerViewDataSource, UIPickerViewDelegate{

   //Add PickerView's Data Source and Data Delegate Methods Here

}

subview Controller 类

class SubViewController: SuperBaseViewController{

   //Whenever you click on PickerView's row than it's delegate method in SuperBaseViewController if you add didSelect method

}

关于swift - 如何在 swift 中使用其他 class() 委托(delegate)和数据源函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41196597/

相关文章:

swift - 如何以一种方式捕获发布者的第一个值,并以另一种方式捕获其余值

ios - swift 3 : How do you wrap content in a tableviewcell with multiple labels?

Xcode 9 中的 Swift 4 - 如何轻量级迁移 Core Data?

xcode - 如何在 TVML 应用程序中嵌入 Facebook 登录?

swift - 按下按钮(快速代码)时如何更改为约束前后?

ios - 如何在 Swift 中同步 tableview 和 searchbar 选择?

ios - Xcode Swift 2 变量声明(实例成员不能用于类型 View Controller )

ios - 如何调整图像大小以适应包含的 ImageView

objective-c - 如何在 Objective-C 代码中重用 Swift 类型别名

ios - Xcode 11.4.0停止运行的单元测试