views:

26

answers:

2

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.

+2  A: 

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];
            }
        }
    }
}
fluchtpunkt
Brilliant, thank you. I'd tried `textField.enabled` for this effect but I think I was doing it too early. With the border and font as well works perfectly. *And* I learnt about self.view.subviews!
mmdeas
A: 

Hello,

As fluchtpunkt answered you can do like that also. Otherwise you can provide one label and one textfield having the same frame size. And on editable true you can hide the label whicle showing the textfield. And for editing false you can show label while hiding the textfield.

Tech Guru