开发者

How to disable/enable all textfields in a viewcontroller

开发者 https://www.devze.com 2023-02-11 03:07 出处:网络
This is more a question about best practices. I have a view with many text fields, and I need a method that will toggle between enabling and disabling all text fields simultaneously. One way that I th

This is more a question about best practices. I have a view with many text fields, and I need a method that will toggle between enabling and disabling all text fields simultaneously. One way that I think will work, is the following:

- (void)activateTextFields:(BOOL)activate {
    [firstTextField setUserInteractionEnabled:activate];
    [secondTextField setUserInteractionEnabled:activate];
    ...
    [lastTextField setUserInteractionEnabled:activate];
}
开发者_运维技巧

However, I'm wondering if there is better way (given the vast number of fields) to do this with just a single line that will disable/enable all text fields at once.

Thanks for the help!


You could place a transparent view on top of all the text fields with userInteraction enabled, that would "trap" all taps and stop them from going to the text fields.

Edit: this is probably better: just return NO from this method:

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.

That way, there's only one bit of state to change, your class's "shouldTextFieldsAllowEditing" boolean property.

0

精彩评论

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