简体   繁体   English

无法动态生成UITextField代替UILabel?

[英]Unable to Generate UITextField dynamically in place of UILabel?

I am trying to generate UITextField dynamically in place of UILabel. 我试图动态生成UITextField代替UILabel。 Now i want to update that data. 现在我想更新这些数据。 I am displaying data in the UILabel from the database and there is an UIButton for editing. 我在数据库中显示UILabel中的数据,并且有一个UIButton用于编辑。 When i click on that UIButton UITextField should be generated in place of UILabel and also data should be displayed in UITextField. 当我点击UIButton时,应该生成UITextField来代替UILabel,并且数据也应该显示在UITextField中。

you need to implement this textField in .h file, to get access to it when you finish aditing. 你需要在.h文件中实现这个textField,以便在完成aditing时访问它。 Then, in your button callback: 然后,在你的按钮回调中:

textField = [[UITextField alloc] initWithFrame:[yourUILabel frame]];
[textField setText:yourUILabel.text];
[self.view addSubView:textField];

then, to replace it back: 然后,将其替换回来:

[yourUILabel setText:textField.text];
[textField removeFromSuperView];

What you can do is to design a view with all textfield which works in two modes, first readonly (by setting userInteraction to false ) and second editing mode. 您可以做的是设计一个视图,其中所有文本域都以两种模式工作,首先是readonly(通过将userInteraction设置为false)和第二种编辑模式。 This way you can avoid the use of labels. 这样就可以避免使用标签。 This will need only one edit button for all of the fields. 这将只需要一个编辑按钮用于所有字段。 if you still want to stick with your approach, you can hide the labels, use their frames to create textfields at their place and make them visible as long as you are working in edit mode. 如果你仍想坚持你的方法,你可以隐藏标签,使用他们的框架在他们的地方创建文本字段,并使他们可见,只要你在编辑模式下工作。 Don't forget to use 别忘了使用

[self.view bringSubviewToFront:TEXT_FIELD];

While you add them to your view. 当您将它们添加到视图中时。 e Managing the editing with the approach I mentioned earlier is mor easy and require less efforts. e使用前面提到的方法管理编辑非常容易,而且需要的工作量更少。 Hope it helps 希望能帮助到你

You can use the methods that others have described here but it sounds like you just want a UITextField that looks like a label and you want to control whether or not it's editable. 您可以使用其他人在此处描述的方法,但听起来您只是想要一个看起来像标签的UITextField,并且您想要控制它是否可编辑。

  • set enabled to YES / NO depending on whether you want the user to edit the UITextField 设置启用为YES / NO,具体取决于您是否希望用户编辑UITextField
  • set the borderStyle of a UITextField (usually between UITextBorderStyleRoundedRect and UITextBorderStyleNone) 设置UITextField的borderStyle(通常在UITextBorderStyleRoundedRect和UITextBorderStyleNone之间)

You can then toggle the enabled and borderStyle values as follows: - on initial view: enabled: NO, border style: UITextBorderStyleNone - on button tap: enabled: YES, border style: UITextBorderStyleRoundedRect 然后,您可以按如下方式切换enabled和borderStyle值: - 在初始视图上:启用:NO,边框样式:UITextBorderStyleNone - 按钮点击:启用:YES,边框样式:UITextBorderStyleRoundedRect

You don't have to mess with the view hierarchy and worry about frames, etc, this way. 您不必混淆视图层次结构并担心帧等,这样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM