Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Providing editable and non-editable fields on the iPhone

I would like to present information to the user of my app in a non-editable way but allow editing after a button press (of the edit button). Is there any way of easily creating this transition from non-editable to editable?

I have considered using UILabels for the non-editable fields and programatically removing them and showing UITextFields instead. However, this would seem to put a lot of the formatting into code which I would prefer not to do (to keep the advantages of IB).

I also considered having both UILabel and UITextField in the same place in my nib and trying to hide the one I don't want. This seems quite hacky though.

Maybe I would just be best off with two separate views?

Any comments on the above methods or better ways of doing it would be very much appreciated.

like image 232
mmdeas Avatar asked Oct 16 '10 11:10

mmdeas


1 Answers

if you set the enabled property of a UITextField to NO and change the borderStyle to UITextBorderStyleNone your textfield looks almost like a UILabel. Maybe you want to toggle those two values.. Something like this:
EDIT: And if you change the font they look exactly like UILabels.

- (IBAction)toggleEdit:(id)sender {
    for (id subview in self.view.subviews) {
        if ([subview isKindOfClass:[UITextField class]]) {
            BOOL isEnabled = ((UITextField*)subview).enabled;
            ((UITextField*)subview).enabled = !isEnabled;
            if (isEnabled) {
                // Disable
                ((UITextField*)subview).borderStyle = UITextBorderStyleNone;
                ((UITextField*)subview).font = [UIFont systemFontOfSize:17.0];
            }
            else {
                // Enable
                ((UITextField*)subview).borderStyle = UITextBorderStyleRoundedRect;
                ((UITextField*)subview).font = [UIFont systemFontOfSize:12.0];
            }
        }
    }
}
like image 103
Matthias Bauch Avatar answered Sep 28 '22 12:09

Matthias Bauch