I'm trying to load a .xib file to act as a custom keyboard when a textField is tapped. I'm able to show the .xib file (view) when the textField is tapped but I'm not sure how to communicate the buttons in the .xib file with the a textField in the ViewController.swift.
This is what I have done so far.
Created a single project.
Added a
UITextFieldand created an outlet for it.@IBOutlet weak var myField: MyTextField!Created a new
.xibfile and called itCustomView.xib.Created a new class and called it
CustomView.swift.Assigned class
CustomView.swiftto theCustomView.xibfile.Added some
UIButtonsin theCustomView.xibfile. These will be acting as a custom-keyboard, they will show when the textField is tapped and hide when theresignFirstRespondermethod is called.In the
viewDidLoadmethod of theViewController.swiftI assignedinputViewas follow.import UIKit class ViewController: UIViewController { @IBOutlet weak var myField: MyTextField! override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. let myView = NSBundle.mainBundle().loadNibNamed("CustomView", owner: self, options: nil).first as? CustomView myField.inputView = myView } }Done
When I run the app and tap on the textField the .xib file shows (see picture below), my problem is, how do I communicate the buttons with the textField in the ViewController.swif. In other words what I want is to show the numbers in the inputField when they are tapped.
Any suggestions? Is this how this is usually done.
Here is an image that shows the view shown when the inputField was tapped.

Let's say you have 9
UIButtons, then consider ctrl + dragging anIBActionfrom all these buttons in storyboard to a single method such as:given that
textFieldis your text field, you could then append each corresponding number from your keypad (that is to say, the buttons) using thetagproperty of these buttons and the aforementioned method.You could set the tag number for each single button in storyboard (I.e., 1 - 9, respectively).
I didn't test it using 9 buttons but did with only 2, with tag numbers 1 and 2, respectively for each buttons. The result was displaying fine in
UITextField(I.e.,1,12,122and so forth).Update (Post-comment):
I was able to re-create this using a
nibfile containing a few buttons and aUITextFieldin storyboard.The process proceeds as follows:
1. Create a nib with all the buttons (which you already have done).
2. Create a view; and under "Custom Class", re-name the class to this view (I.e., "Custom Class" -> "Class" -> "view-that-holds-the-buttons").
3. Wire the
IBActions (a total of 9 corresponding to the number of your buttons) to one single method as described above.4. In your other view controller whose view hold the
UITextField, load the nib using your existing method.5. Add this view (from nib) as subview.
The following concerns with communication between the view (that holds the buttons along with the
IBActionmethod) and the controller in which you load thenib:6. create a
delegate(weak) property.7. Before you add the view (from
nib), assign thisdelegatewith the view controller (the control that loads thenib).8. Create a protocol:
For instance:
Have the view controller that loads the
nibconform to this protocol and implement the required method (displayKeys):So, once the buttons are tapped, the
IBActionwould be called; but instead of displaying it, we send thesenderto ourdelegate, which is the view controller that implements thedisplayKeysmethod and holds theUITextField.The
IBActionwould be implemented as follows:displayKeyswould be implemented like the following:Declaration of the
delegatein the controller where you load thenibfile:Assigning the
delegatefrom within the view controller where you load thenib:In reply to your second comment:
Assumption:
We have 2 classes.
The first one is a subclass of
UIView, which is thexib, that holds the buttons. Let’s call this „KeypadView“.The second one is the main view controller, which is associated to the controller that holds the
UITextFieldin your storyboard. Let’s call this „MainViewController“.Step 2:
Firstly, please create a new
UIViewand name it, for the sake of consistency, „KeypadView“.Then, click on your .xib file; on the right panel, click on the third tab from the left, which is called „Identity Inspector“; you would see „Custom Class -> Class“, where you would associate this
xibto the class you created (you need this class, in order to connect theIBActionfor the buttons from thexibfile to it). It would be the „KeypadView“, which is a subclass ofUIView.Step 6:
You declare this in the class („KeypadView“) that holds the buttons.
Step 8:
You connect this method (
IBAction) to the aforementioned class (I.e., „KeypadView“).Once you load the
xib(„KeypadView“) from within the „mainViewController“, set thedelegatein „KeypadView“ toself(selfis the „MainViewController“):In your „KeyPadView“ class, there should be an
IBActionthat gets called from each of the buttons:I.e.,
Our
delegateis the „mainViewController“, if you recall.Since
displayKeysis implemented in „mainViewController“, the following method would be called therein:„mainViewController“ would then display the keystrokes in its
UITextField(I.e.,textView).