Search code examples
iosswiftuitextfielduipickerview

two uitextfield i want one to be disabled so it cannot be edited - SWIFT


I have two UITextField one is a normal textfield which requires text to be inputted. The other is connected to a UIPickerView. When i click the UITextField which is connected to the pickerView a cursor appears, the textfield is not editable as the keyboard is the pickerView, but text can be copied and pasted into it. Any Help??

I tried to fix it through the UITextFieldDelegate method

       func textFieldShouldBeginEditing(textField: UITextField) -> Bool { }

But it controls both of my textfields and doesn't resolve my issue.

Thanks in advanced!!!


Solution

  • Two things:

    First, when you implement delegate methods like that, you have to return a different value based on which text field is passed in. One relatively easy way to do this is to specify a numerical tag for each text field in Interface Builder, and then in your delegate method, compare the passed-in field's tag property to the expected values.

    Second, you probably should do this by just setting the enabled property on the text field to NO instead of implementing a delegate method and trying to prevent editing.