I've spent hours trying to figure out how to create/then get a custom inputView
to work.
I have a grid of TextInputs
(think scrabble board) that when pressed should load a custom inputView
to insert text.
I've created a .xib file containing the UI elements
for the custom inputView
. I was able to create a CustomInputViewController
and have the inputView
appear but never able to get the actual TextInput
to update it's value/text.
Apple documentation has seemed light on how to get this to work and the many tutorials I've have seen have been using Obj-C (which I have been unable to convert over due to small things that seem unable to now be done in swift).
What is the overarching architecture and necessary pieces of code that should be implemented to create a customInputView
for multiple textInputs
(delegate chain, controller, .xibs, views etc)?
Set up a nib file with the appropriate inputView layout and items. In my case I set each button to an action on File Owner of
inputViewButtonPressed
.Set up a storyboard (or nib if you prefer) for a view controller.
Then using the following code, you should get what you're looking for:
Alternatively, if you're wanting to be more keyboard-like, you can use this function as your action (it uses the new let syntax from Swift 1.2), break it up if you need 1.1 compatibility:
This uses the
UITextInput
protocol to update the text field as appropriate. Handling delete is a little more complicated, but still not too bad:You shouldn't go through all that hassle. There's a new class in iOS 8 called:
UIAlertController
where you can add TextFields for the user to input data. You can style it as an Alert or an ActionSheet.Example:
Now that you have the controller, add fields to it:
Add action buttons:
Present the controller whenever you want with:
As you see, you have various handlers to pass custom code at any step.
As example, this is how it would look: