Michael Paniccia
Michael Paniccia

Reputation: 931

how to block user from changing text field

I have two text fields that I don't want the user to change. Is there a way to do this in Xcode 6? I have already entered text in the text field but when I simulate the app the user is still able to modify it.

Upvotes: 1

Views: 238

Answers (3)

SukruK
SukruK

Reputation: 560

Return NO for above method

   optional func textField(_ textField: UITextField,
    shouldChangeCharactersInRange range: NSRange,
                replacementString string: String) -> Bool

Or set textfield disable and userInteractionDisable

textfield.userInteractionEnabled = false
textfield.enabled = false

Upvotes: 0

Bhavesh
Bhavesh

Reputation: 1422

Make category on UIView.

add following methods in that category.

-(void)disable {
    self.alpha = 0.5;
    self.userInteractionEnabled = FALSE;
}

-(void)enable {
    self.alpha = 1.0;
    self.userInteractionEnabled = TRUE;
}

then just call

yourTextField.disable();

Actually,You can use this on any view that you want.

Upvotes: 0

zaph
zaph

Reputation: 112857

Return 'NO' for the delegate method:

`- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField`.

or disable the textField, that may alter the appearance.

textField.enabled = NO

or use a UILabel.

Upvotes: 1

Related Questions