[英]Is there a way to display additional textFields if a user clicks on a button inside a single view controller?
I have a fairly complex form inside a view controller that requires a few text fields to be filled out for some users and many other text fields for other users. 我在视图控制器中有一个相当复杂的表单,该表单需要为一些用户填写一些文本字段,而为其他用户填写许多其他文本字段。 Is there a way to start the view controller with a couple of text fields, and then if a user clicks on a button('MORE')...the other text fields will display? 有没有一种方法可以使用几个文本字段来启动视图控制器,然后如果用户单击按钮(“更多”)...将显示其他文本字段?
You can add all the textFields in the View in Interface Builder and set the attribute myTextField.hidden = true
. 您可以在Interface Builder中的View中添加所有textField,并设置属性myTextField.hidden = true
。 When you click the more
button then you can set the hidden attribute to false. 单击more
按钮时,可以将hidden属性设置为false。
Hope that helps. 希望能有所帮助。
Is there a way to start the view controller with a couple of text fields, and then if a user clicks on a button('MORE')...the other text fields will display? 有没有一种方法可以使用几个文本字段来启动视图控制器,然后如果用户单击按钮(“更多”)...将显示其他文本字段?
Sure. 当然。 When the user taps the button, you can insert the additional fields. 当用户点击按钮时,您可以插入其他字段。 You might do that by creating the new fields and inserting them into the existing view, or loading some different view and setting the view controller's view to that new view (and of course populating the new view with the user's data). 您可以通过创建新字段并将其插入到现有视图中,或者加载一些不同的视图并将视图控制器的视图设置为该新视图(当然还要用用户的数据填充新视图)来做到这一点。 Or the fields in question could exist in the original view but be hidden. 或相关字段可能存在于原始视图中但被隐藏了。
A view controller is fully in charge of the view it manages, including all subviews of that view, so it can really do whatever it likes to move, add, or remove views, controls, etc. 一个视图控制器完全负责它所管理的视图,包括该视图的所有子视图,因此它可以真正完成移动,添加或删除视图,控件等所需的操作。
Yes you can. 是的你可以。 You can add text field at runtime by dynamically creating them. 您可以在运行时通过动态创建文本字段来添加文本字段。 But there is another possibility to design these fields in a separate UIView
using IB and add this UIView
to ViewController.view
whenever needed. 但是还有另一种可能性,可以使用IB在单独的UIView
设计这些字段,并在需要时将此UIView
添加到ViewController.view
。 This way you will save the positioning effort of each textfield element. 这样,您将节省每个文本字段元素的定位工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.