ios - 如何为动态选择器 View 创建函数

标签 ios swift function

我创建了一个 pickerview,它在我的屏幕上点击一个文本字段后打开。查看 pickerview 的代码,您可以看到它只有两个输入:它应该属于的文本字段导出,以及一个填充 pickerview 的数组。它还有一个漂亮的工具栏,可以保存输入或让您转到下一个输入字段。

在下面的代码中,您可以看到我是如何设置它的。

class PersonalDetails: UITableViewController, UIPickerViewDelegate {

@IBOutlet weak var questionLabel: UILabel!
@IBOutlet weak var pickerTextFieldCountry: UITextField!

override func viewDidLoad() {
    super.viewDidLoad()

    var pickOptionCountry = ["Belgium", "France", "Germany", "Netherlands", "Sweden"]

    let pickerView = UIPickerView()
    pickerView.delegate = self
    pickerView.backgroundColor = UIColor.whiteColor()
    pickerView.showsSelectionIndicator = true

    let toolBar = UIToolbar()
    toolBar.barStyle = UIBarStyle.Default
    toolBar.translucent = true
    toolBar.tintColor = UIColor(red: 76/255, green: 217/255, blue: 100/255, alpha: 1)
    toolBar.sizeToFit()
    let doneButton = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.Plain, target: self, action: "donePicker")
    let spaceButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: nil, action: nil)
    let nextButton = UIBarButtonItem(title: "Next", style: UIBarButtonItemStyle.Plain, target: self, action: "nextPicker")
    toolBar.setItems([doneButton, spaceButton, nextButton], animated: false)
    toolBar.userInteractionEnabled = true

    pickerTextFieldCountry.inputView = pickerView
    pickerTextFieldCountry.placeholder = "Not Set"
    pickerTextFieldCountry.inputAccessoryView = toolBar
    if let text = NSUserDefaults.standardUserDefaults().stringForKey("test") {pickerTextFieldCountry.text = text}
}

override func didReceiveMemoryWarning() {super.didReceiveMemoryWarning()}


// MARK - PART TWO: HOW TO INTEGRATE THE FUNCTIONS BELOW?
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
    return pickOptionCountry.count}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
    return pickOptionCountry[row]}
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
    pickerTextFieldCountry.text = pickOptionCountry[row]}
func nextPicker() {
    NSUserDefaults.standardUserDefaults().setValue(pickerTextFieldCountry.text, forKey: "test")
    pickerTextFieldCountry.nextResponder()}
func donePicker() {
    NSUserDefaults.standardUserDefaults().setValue(pickerTextFieldCountry.text, forKey: "test")
    pickerTextFieldCountry.resignFirstResponder()}

因为我计划制作大约 20 个相同的输入字段,所以我希望制作一个动态函数,这样我就不必一次又一次地重写相同的代码。最好我应该能够通过 func(textfieldID,arrayID) 调用该函数。在 didReceiveMemoryWarning 之后尝试合并 pickerView 函数时我遇到了困难(请参阅 MARK - 第二部分)。

最佳答案

这正是这些函数的用途。您可以通过直接在选择器中编写逻辑来使用它们显示任何类型的数据。例如,您可以对 numberOfRowsInComponent 执行以下操作:

func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
   switch aVariableIdentifyingTheFiledPressed {
    case 0:
      return pickOptionCountry.count
    case 1:
      return anotherOption.count
   }
}

您甚至可以利用 swift 的强大功能并使用您的所有选项创建一个枚举

enum Planet {
   case optionCountry, anotherOption
}

然后在枚举上构建开关。更酷的是,您可以直接将每个选项的选择作为原始值放入枚举中!

关于ios - 如何为动态选择器 View 创建函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33899210/

相关文章:

ios - 使用 uisearchbar 搜索后如何获取索引部分标题

ios - 如何仅在 iOS 中获取日期而不是时间

ios - Swift Singleton Init 在 XCTest 中被调用两次

c++ - 函数指针 - 类的内部和外部

c++ - 使用 C++ 函数将二维数组转换为文本

r - 子集一个附加变量并将其附加到 R 中的前一个变量

备份 iOS 组容器?

ios - Swift 中与 DispatchQueue 同步处理

ios - 更改部分页脚的背景颜色

ios - UserDefaults 的 Switch 语句(Swift-Xcode)