开发者

UITextField is always editable

开发者 https://www.devze.com 2023-04-01 10:00 出处:网络
I\'ve dropped a UITextField control on one of my xibs and I want it to be un-editable.I\'ve \"unchecked\" both the \"enabled\" and \"User Interaction Enabled\" on the \"Attributes Inspector.\"However,

I've dropped a UITextField control on one of my xibs and I want it to be un-editable. I've "unchecked" both the "enabled" and "User Interaction Enabled" on the "Attributes Inspector." However, when I run this application in the simulator, when I tap on the TextFiel开发者_如何转开发d the keyboard always appears. I've searched around and these two attributes seem to be what controls the "edit-ness" of this particular control. I'm able to create an UITextArea, as there exists an "Editable" checkbox that I "unchecked." However, there is no "Editable" checkbox in the inspector for a UITextField. Am I out of luck? Or is there more setup required that I'm not aware of?


Try with this:

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


The "Enabled" property is the one you're looking for if you want to disable touch events from the user.

see: http://developer.apple.com/library/ios/#documentation/uikit/reference/UIControl_Class/Reference/Reference.html#//apple_ref/occ/cl/UIControl

It's possible you're having some trouble with the linkage between your Interface Builder and your app (I've had this problem several times).

Try setting the value directly in your code instead of using the IB and see if it gives you the desired results.


Try this, in your viewDidLoad set your textField to this:

myTextField.userInteractionEnabled = FALSE;


yourTextFieldName.enabled = NO;

This seems to work pretty well for me, hope this helps.


After pulling my hair out due to none of the solutions working (checkboxes and adding code to disable the TextField), what finally solved the problem was deleting the control, and then re-adding it. Once it was re-added, I was able to disable the TextField with either the "Enabled" checkbox or assigning "NO" to textField.enabled. I have no idea why it works now, but this can be a solution to others that are having the same problem.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号