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
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.
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:
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.