简体   繁体   中英

set UITextField as non editable - Objective C

    [Number.editable = NO];
    [Number resignFirstResponder];
    [Password.editable = NO];
    [Password resignFirstResponder];

I am getting the error

Request for member 'editable' in something not a structure or union

:S

Thanks

Firstly, the [...] aren't needed if you're not sending a message.

Number.editable = NO;
[Number resignFirstResponder];
Password.editable = NO;
[Password resignFirstResponder];

But this is not the cause of error. The .editable property is only defined for UITextView, not UITextField. You should set the .enabled property for a UITextField (note that a UITextField is a UIControl).

Number.enabled = NO;
...

Also, you can use the delegate methods.

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
  return NO;
}

That would do the trick, I prefer this method over setting textField.enabled = YES when it's likely that the ability to edit will change during the lifecycle of the app.

textField.userInteractionEnabled = NO;

希望这可以帮助..

从shouldChangeCharactersInRange返回NO将是更好的选择,因为如果文本长于文本字段宽度,那么上面的解决方案将给出问题,因为用户将无法看到所有文本(即隐藏在文本字段宽度之外的文本)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM