确定文本字段是否具有焦点

时间:2016-05-23 20:03:48

标签: swift if-statement uipickerview

我正在为ios应用程序编写if语句,我想根据当前具有焦点的文本字段更改选择器。这是我到目前为止所做的。

func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
    return 1
}

func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {

    if (condition) {
        return array1.count
    }else{
        return array2.count
    }
}

func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {

    if (condition) {
        textBox1.text = array1[row]
    }else{
        textBox2.text = array2[row]
    }
}

func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {

    if (condition) {
        return array1[row]
    }else{
       return array2[row]
    }
}

2 个答案:

答案 0 :(得分:0)

您想要UITextField.isFirstResponder()

答案 1 :(得分:0)

好吧,虽然我没有在代码中看到任何与文本字段相关的代码,但我们假设你有一个名为myTxt的UITextfield的IBOutlet。触摸时的代码:

myTxt.addTarget(self, action: #selector(CurrentViewController.MethodToUpdatePicker()), forControlEvents: UIControlEvents.TouchUpInside)

以及当值发生变化时的情况:

myTxt.addTarget(self, action: #selector(CurrentViewController.MethodToUpdatePicker()), forControlEvents: UIControlEvents.EditingChanged)

只需用您的函数替换CurrentViewController.MethodToUpdatePicker()即可更新选择器。

以下是来自apple的更多文档:

https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIControl_Class/#//apple_ref/occ/instm/UIControl/addTarget:action:forControlEvents

相关问题