1

I wanna show a picker when I click inside a UITextField. This is my code (which works):

 [super viewDidLoad];
// Picker View dei Tipi
pickerTipo= [[UIPickerView alloc] init];
pickerTipo.showsSelectionIndicator = YES;
UIToolbar* keyboardDoneButtonView = [[UIToolbar alloc] init];

[keyboardDoneButtonView sizeToFit];
UIBarButtonItem* doneButton = [[UIBarButtonItem alloc] 
                               initWithTitle:
                               @"Fine"  style:UIBarButtonItemStyleBordered 
                               target:self 
                               action:@selector(pickerDoneClicked:)];

[keyboardDoneButtonView setItems:[NSArray arrayWithObjects:doneButton, nil]];

self.textTipo.inputAccessoryView = keyboardDoneButtonView;
[self.textTipo resignFirstResponder];
self.textTipo.inputView = pickerTipo;
self.textTipo.delegate  = self;

The picker and the accessory view show correctly. The problem is that the UITextField is still editable, that is I can still typing inside the UITextField. I would that when the picker is shown, no editing is enabled inside the UITextField.

[SOLVED} Following the accepted answer, the right code is :

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range   replacementString:(NSString *)string
{
 if (textField == self.textTipo || textField == self.textStato)
    return NO;
 else
     return YES;
} 
gdm
  • 7,647
  • 3
  • 41
  • 71

4 Answers4

3
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{

    if([textField.restorationIdentifier isEqualToString:@"textFieldIdentifier"])
    {
        return NO;
    }
}

Hope it helps.

keen
  • 3,001
  • 4
  • 34
  • 59
  • This is right. I would also return YES or NOT, w.r.t to the textField which is tied to the picker. – gdm Jun 17 '13 at 11:59
2

Instead of textfied you can use button with a background uiimage view of text field.

Phantomcho
  • 302
  • 1
  • 6
1

try this code:

-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField;
         {
                 if(textField==self.textTipo)
                  {

                        return NO;
                   }
                  else
                  {
                      return YES;
                   }

          }
kirti Chavda
  • 3,029
  • 2
  • 17
  • 29
-1

make your textfield a readonly property

Baddu
  • 290
  • 2
  • 11
  • This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post - you can always comment on your own posts, and once you have sufficient [reputation](http://stackoverflow.com/faq#reputation) you will be able to [comment on any post](http://stackoverflow.com/privileges/comment). – Anders R. Bystrup Jun 17 '13 at 12:14
  • this had helped me that's why i suggested this otherwise Anders R. Bystrup, u suggest a answer, i will also accept that if it helps – Baddu Jun 17 '13 at 12:16