1

当我在 UITextField 内单击时,我想显示一个选择器。这是我的代码(有效):

 [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;

选择器和附件视图正确显示。问题是 UITextField 仍然是可编辑的,也就是说我仍然可以在 UITextField 中输入。我会的,当显示采摘器时,Uitextfield内部未启用编辑。

[已解决}按照接受的答案,正确的代码是:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range   replacementString:(NSString *)string
{
 if (textField == self.textTipo || textField == self.textStato)
    return NO;
 else
     return YES;
} 
4

4 回答 4

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

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

希望能帮助到你。

于 2013-06-17T10:47:41.770 回答
2

您可以使用带有文本字段背景 uiimage 视图的按钮来代替 textfied。

于 2013-06-17T11:43:02.260 回答
1

试试这个代码:

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

                        return NO;
                   }
                  else
                  {
                      return YES;
                   }

          }
于 2013-06-17T10:46:36.077 回答
-1

使您的文本字段成为只读属性

于 2013-06-17T11:51:32.690 回答