UIBUTTON 和选择器视图
我想在用户单击按钮时显示选择器 我对 UIButton 进行了子类化并更改了其输入视图,使其可写。
我点击了此链接 http://nomtek.com/tips-for-developers/ working-with-pickers/
我点击按钮,但没有任何反应。如果我将输入视图从按钮更改为 UITextField ,效果会很好。有什么想法吗? 谢谢 萨罗
I want to display a picker when the user clicks on a button
I subclassed the UIButton and changed its inputview so that it is writable.
I followed this link http://nomtek.com/tips-for-developers/working-with-pickers/
I clickon the button and nothing happens. If I change the inputview from the button to UITextField it works great. Any thoughts?
Thanks
Saro
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
1) 创建 UIButton
.h
.m
的子类2) 在视图控制器(选择器或任何其他自定义视图)中设置
inputView
(如果需要,还可以设置inputAccessoryView
)3) 在视图控制器中为按钮创建 IBAction
如果要删除键盘,请调用按钮:
或在视图控制器中:
1) Create a subclass of a UIButton
.h
.m
2) Set
inputView
(if needed alsoinputAccessoryView
) in your view controller (to your picker or any other custom view)3) Create IBAction for your button in your view controller
If you want to remove keyboard, call on your button:
or in view controller:
这就是我在 Swift 3 中所做的(重写属性的规则与 Objective-C 不同)。
我想要一个显示日期的按钮,并在点击按钮时从屏幕底部显示日期选择器,以允许用户编辑显示的日期。使用锚定在目标按钮上的弹出窗口相对容易做到这一点,但我的应用程序仅适用于 iPhone(不是 iPad),因此 UITextField-inputView 模式似乎更好(在 AppStore 应用程序上使用,您需要承担违反人类行为准则的风险)接口指南)。
按钮子类:
视图控制器代码:
注意:
为简单起见,此代码将在指定日期后立即关闭日期选择器 - 即,每当您滚动任何滚轮时。这可能不是您想要的:通常,用户一次指定一个日期组成部分(年、月等)。此实现不会让用户在关闭日期选择器之前完成指定所有日期组件。
理想情况下,您可以添加一个
UIToolbar
作为输入附件视图,并带有一个“完成”按钮来关闭输入视图(即,导致该按钮放弃第一响应者)。这将需要进一步修改 UIButton 子类(以适应 inputAccessoryView),但应该是可行的。编辑:我可以确认输入附件视图也可以以与输入视图完全相同的方式附加到自定义 UIButton 子类:
现在,我将其设置如下:
并用两个单独的响应进行响应操作:
此外,当视图控制器从导航中弹出时,我的自定义按钮子类的
deinit()
方法确实会被调用,因此UIButton
不再(?)保留其目标,或者 Swift 运行时比我想象的更聪明...更新:我组合了一个 Xcode 项目演示此自定义按钮(Github 存储库)。修改它以使用自定义
UIPickerView
(而不是UIDatePicker
)应该非常简单。 不立即显而易见的是(至少对我而言)如何使其与键盘作为输入视图一起使用。This is how I did it in Swift 3 (the rules for overriding properties are different from Objective-C).
I wanted a button that displays a date, and to display a date picker from the bottom of the screen on button tap, to allow the user to edit the date displayed. This is relatively easy to do with a popover anchored over the target button, but my app is iPhone-only (not iPad) so the UITextField-inputView pattern seems better (use on AppStore apps at your own risk of violating the Human Interface Guidelines).
Button subclass:
View Controller Code:
Note:
For simplicity, this code will dismiss the date picker as soon a date is specified -i.e., whenever you scroll any of the wheels. This might not be what you want: Typically, the user specifies a date one component at a time (year, month, etc.). This implementation won't let the user finish specifying all date components before dismissing the date picker.
Ideally, you would add a
UIToolbar
as an input accessory view, with a "done" button that dismisses the input view (i.e., causes the button to resign first responder). That would require further modification of the UIButton subclass (to also accommodate an inputAccessoryView), but should be doable.EDIT: I can confirm that an input accessory view can also be attached to the custom UIButton subclass in the exact same way as the input view:
Now, I set it up like this:
And respond with two separate actions:
Also, the
deinit()
method of my custom button subclass does get called when the view controller is popped out of the navigation so eitherUIButton
no longer (?) retains its targets, or the Swift runtime is smarter than I thought...UPDATE: I put together an Xcode project demonstrating this custom button (Github repository). It should be quite straightforward to modify it to work with a custom
UIPickerView
(instead of aUIDatePicker
). What is not immediately obvious (to me, at least) is how to make it work with a keyboard as input view.inputView
属性只是UITextField
和UITextView
的一部分,而不是UIButton
。尽管您可以使用
UIButton
实现相同的效果。创建一个
UIButton
对象,然后设置一个 action 方法,并在 UIButton 的操作方法中将选择器视图添加到按钮的父视图中。应该是这样的。
The
inputView
property is only the part ofUITextField
andUITextView
notUIButton
.Although you could achieve the same using
UIButton
.Create an
UIButton
object then set an action method and add your picker view in parent view of your button in the action method of UIButton.Should be something like that.