开发者

How to disable/enable all textfields in a viewcontroller

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜