19

I understand this question has been asked before, but I'm not satisfied with the answers, i.e. by making it disabled. There is a fundamental difference: Disabled view doesn't fire events, but for a read-only view, it should still fire event like (e.g. TouchUpInside), and I need it. Only thing I don't want is the keyboard input.

The reason is that I have several input fields, some can useUITextField directly, others are not. I want to have them look similar. So, I'd like to use UITextField to display all of them. Some of them need to be read-only so that I can use touch up event for alternative input.

Or there might be a completely different way to do it?

EDIT: This is for my MonoTouch project. I have very limited knowledge of Objective-c.

1
  • It's way easy to assign a common tag value to all the 'non-editable' textFields and check it on the 'textFieldDidBeginEditing' method and return the BOOL appropriately
    – Gokul
    Commented Jun 24, 2014 at 4:59

6 Answers 6

32

Say you have 2 text field instance variables connected to text fields you created in the Interface Builder. Lets call them myReadOnlyTextField and myEditableTextField. Make sure you connect the delegate property of each text field in the Interface Builder to the view controller ("File's Owner")[1]. Now, in the view controller @implementation (.m file), use the method textFieldShouldBeginEditing: and put in some logic to determine which text field you want to allow editing and which to not allow editing; something like this:

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    BOOL editable;
    if (textField == myReadOnlyTextField) {
        editable = NO;
    } else if (textField == myEditableTextField) {
        editable = YES;
    } else {
        // editable = YES/NO/Other Logic
    }
    return editable;
}

From the UITextFieldDelegate Documentation:

textFieldShouldBeginEditing:
Asks the delegate if editing should begin in the specified text field.

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField

Parameters
textField - The text field for which editing is about to begin.

Return Value
YES if an editing session should be initiated; otherwise, NO to disallow editing.

Discussion
When the user performs an action that would normally initiate an editing session, the text field calls this method first to see if editing should actually proceed. In most circumstances, you would simply return YES from this method to allow editing to proceed.

Implementation of this method by the delegate is optional. If it is not present, editing proceeds as if this method had returned YES.

UITextField Documentation is a good read also.


[1] You can do this programmatically as well. Here is an example:

- (void)viewDidLoad {
    [super viewDidLoad];
    // .....
    myReadOnlyTextField.delegate = self;
    myEditableTextField.delegate = self;
}
6
  • 1
    Thank you very much for your quick response. I'm sorry I forgot to mention in my question that I'm working in MonoTouch project. From my limited object-c knowledge, I don't understand what you are trying to do here. Actually, I'm looking for a simple solution (like setting a property) if possible at all. If it takes so much code to make it work, I will probably try to find a different solution.
    – newman
    Commented Oct 31, 2011 at 2:20
  • 1
    A dozen or so lines of code is just to much for you? And by the way you are setting a property, the delegate property. And that delegate decides weather you can edit the field.
    – NJones
    Commented Oct 31, 2011 at 2:29
  • 8
    @miliu, No offense, but this is the correct answer. Why not take the time to implement it and perhaps learn some objective-c while doing it?
    – sosborn
    Commented Oct 31, 2011 at 2:32
  • The only property related to not allowing input in a text field would be to set enabled property to NO but like you said, that would "disable" the text field all together, preventing events from firing. But, if you implement the textFieldShouldBeginEditing: method, it is automatically called whenever someone touches the input box and whatever you return from that method is what determines if text can be entered in the field or not. Running out of comment space... see my next comment...
    – chown
    Commented Oct 31, 2011 at 2:33
  • I dont have any experience with MonoTouch, but check out these tutorials: icodeblog.com/2008/07/30/… jomnius.blogspot.com/2010/07/… roseindia.net/tutorial/iphone/examples/… iphonesdkdev.blogspot.com/2009/01/…
    – chown
    Commented Oct 31, 2011 at 2:33
15

Despite the fact you need no keyboard, textField:shouldChangeCharactersInRange:replacementString: is very useful. It prevents text field from editing but still leaves it selectable in contrast to textFieldShouldBeginEditing:.

In monotouch:

var txt = new UITextField();
txt.ShouldChangeCharacters += (field, range, replacementString) => false;
1
  • 2
    This is useful when replacing the input view (keyboard) with a custom view (eg. a date picker).
    – andref
    Commented Aug 26, 2012 at 22:37
5

You can have two options: a) to use ShouldBeginEditing with return false, but you can't use PickerView as InputView in your text field. b) to use ShouldChangeCharacters that will prevent the editing but will allow to use InputView with PickerView.

2

Objective C:

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {

  return NO;
}

Swift :

func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {

  return false
}
1

because you are working in MonoTouch you could use the following line of code:

myReadOnlyButton.ShouldBeginEditing = t =>
{
  //event code
  return false;
};
-1

I personally use borderStyle = .none + userInteractionEnabled = false to make it look like a regular label.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.