开发者

Providing editable and non-editable fields on the iPhone

开发者 https://www.devze.com 2023-01-20 01:27 出处:网络
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 no

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 t开发者_StackOverflowhe above methods or better ways of doing it would be very much appreciated.


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];
            }
        }
    }
}


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.

0

精彩评论

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