在 iPhone 上提供可编辑和不可编辑字段

发布于 2024-09-27 21:36:02 字数 324 浏览 3 评论 0原文

我想以不可编辑的方式向我的应用程序的用户呈现信息,但允许在按下按钮(编辑按钮)后进行编辑。有没有什么方法可以轻松创建从不可编辑到可编辑的过渡?

我考虑过将 UILabel 用于不可编辑的字段,并以编程方式删除它们并显示 UITextField。然而,这似乎会将大量格式放入代码中,而我不想这样做(以保持 IB 的优势)。

我还考虑将 UILabelUITextField 放在笔尖的同一位置,并尝试隐藏我不想要的那个。但这看起来很老套。

也许我最好有两个不同的观点?

任何对上述方法或更好的方法的评论将非常感激。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

凝望流年 2024-10-04 21:36:02

如果您将 UITextField 的启用属性设置为 NO 并将 borderStyle 更改为 UITextBorderStyleNone 您的文本字段看起来几乎像 UILabel。
也许您想切换这两个值..像这样:
编辑:如果你更改字体,它们看起来就像 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];
            }
        }
    }
}

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];
            }
        }
    }
}
难如初 2024-10-04 21:36:02

正如fluchtpunkt 回答的那样,你也可以这样做。否则,您可以提供具有相同框架大小的一个标签和一个文本字段。在可编辑 true 上,您可以隐藏显示文本字段的标签。对于编辑 false,您可以在隐藏文本字段的同时显示标签。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文