UITextField中的UIPicker

时间:2013-06-17 10:42:22

标签: ios uipicker

当我在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 个答案:

答案 0 :(得分:3)

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

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

希望它有所帮助。

答案 1 :(得分:2)

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

答案 2 :(得分:1)

试试这段代码:

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

                        return NO;
                   }
                  else
                  {
                      return YES;
                   }

          }

答案 3 :(得分:-1)

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