将 UITextField 的 inputAccessoryView 设置为其超级视图
我有一个带有“添加单元格...”行的 UITableView,并且希望弹出一个键盘,上面有一个视图,就像在“消息”应用程序中一样,用户可以在其中键入新单元格的名称。
我意识到还有几种其他方法来获取用户数据,以及多种方法来实现我想要实现的功能。
例如,当应创建新的播放列表时,iPod 应用程序会显示一个弹出窗口。
现在,我有一个隐藏文本字段,当按下“添加单元格...”行时,该字段设置为第一个响应者,并且我将包含输入字段和确认按钮的视图指定为该隐藏字段的 inputAccessoryView。或者,我可以将此视图添加为表的子视图,并使用键盘通知定位它。
我只是想知道是否有一种更清晰的方法来完成我想要做的事情(例如将输入文本字段的 inputAccessoryView 设置为显示为文本字段的超级视图)。我尝试了几种方法,但当视图应该关闭时,我似乎无法使用 resignFirstResponder 关闭键盘。我可以让 inputAccessoryView 消失,但键盘仍然保持打开状态,占用必要的屏幕空间。另外,当笔尖的视图是一个以 UITableViewController 作为文件所有者的 UITableView,而不是一个以 UIViewController 作为文件所有者的 UIView 时,我会收到一个非常奇怪的错误:“设置表的第一响应者视图,但我们不知道其类型(单元格/页眉/页脚)”
提前致谢, 朱利安·塞佩克
I have a UITableView with an Add cell... row and would like to have a keyboard pop up with a view above it like in the "Messages" application, where the user can type the name of the new cell.
I realize that there are several other ways to get user data, and several ways to implement the functionality that I am trying to achieve.
For example, the iPod application presents a popup when a new playlist should be created.
Right now, I have a hidden text field that is set to be the first responder when the Add cell... row is pressed, and I assign the view containing the input field and confirmation button as the inputAccessoryView for this hidden field. Alternatively, I could add this view as a subview of the table and position it using keyboard notifications.
I would just like to know if there is a cleaner way to accomplish what I am trying to do (such as setting the inputAccessoryView of the input textField to be displayed to be the textField's superview). I have tried several approaches, but I cannot seem to be able to dismiss the keyboard using resignFirstResponder when the view should close. I can get the inputAccessoryView to disappear, but the keyboard remains resolutely open, taking up necessary screen real estate. Also, when the nib's view is a UITableView with a UITableViewController as the File's Owner rather than a UIView with a UIViewController as the File's Owner, I get a very odd error: "setting the first responder view of the table but we don't know its type (cell/header/footer)"
Thanks in advance,
Julian Ceipek
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
通过 UITextView/UITextField 类上的
setInputAccessoryView
,您的方向是正确的。此方法允许您将想要的任何视图添加到键盘顶部。然后,您创建的视图将使用委托方法来告诉主视图控制器 resignFirstResponder。
因此,为了让您开始:
实现可能看起来有点像(仅发布生成视图的代码):
我使用了标准的导航栏外观视图,但您可以放入任何您喜欢的内容,包括按钮、文本字段、图像机器人独角兽,工作
如果您没有了解上面代码中发生的所有内容,您可能需要温习委派并以编程方式创建视图。
You are on the right track with the
setInputAccessoryView
on the UITextView/UITextField classes' This method allows you to add any view you want to the top of a keyboard.The view you create would then use a delegation method to tell the main view controller to resignFirstResponder.
So, to get you started:
The implementation might look a little like (only posting the code that makes the view):
I have used a standard NavigationBar looking view, but you could put in anything you like and include buttons, textfields, images of robot unicorns, the works
If you don't get everything thats going on in the above code you might need to brush up on Delegation and creating views programmatically.