Custom keyboard in swift iOS 10 - swift

How do I create outlets for my keys in a custom keyboard to switch from letters to numbers and symbols? I am trying to create a custom keyboard and I don't know how to create outlets for my keys to switch back and forth from letters to numerals and symbols.

So, I gather from your comment, that this is what you are looking for. The type of keyboard input, is determined by setting the 'keyboardType' property of the keyboard's delegate. If you want to switch and update a view, I think you'll have to dismiss the keyboard and recall it. I find this unsafe, and would highly suggest using a normal keyboard (which has all the characters you need), trusting your user, and sanitizing the input. But the following does work, if you must. In the following, 'txtField' is a UITextField with the UIViewController set as its delegate, the IBAction is wired to a UIButton, and 'isNumbers' is a Bool property of the UIViewController.
#IBAction func switchKeyboards(_ sender: Any) {
if isNumbers {
txtField.keyboardType = UIKeyboardType.alphabet
isNumbers = false
} else {
txtField.keyboardType = UIKeyboardType.numberPad
isNumbers = true
}
txtField.resignFirstResponder()
txtField.becomeFirstResponder()
}

Related

Keyboard does not open when clicking on UITextfield

I create sign in page with swift programmatically coding. I tried simulator and device but keyboard did not opened when clicked on textfied.
when I write code in viewController with stroyboard, keyboard had opened. But, without using storyboard, keyboard is not opening.
Did you set the TextField's delegate? In the Storyboard you must have, but in code that is sometimes forgotten.
class ViewController: UIViewController, UITextFieldDelegate {
let textField = UITextField()
func viewDidLoad() {
super.viewDidLoad()
textfield.delegate = self // Did you do this?
}
}
If you are using simulator then you must go to I/O -> Keyboard -> Toggle software keyboard.
If you are using physical device then through code you need to set as as first responser as below:
textfieldName.becomeFirstResponder()

Dismissing a numeric keyboard

A noob question here: I have read a lot about dismissing the keyboard from objects like labels or event when one taps the RETURN key or other areas of the screen. However, is there code to dismiss a keyboard from within a BUTTON? I tried various ways of ResignFirstResponder at the end of the code in my BUTTON but to no success.
Any help that can be shed would be appreciated. Please remember I a new to all this so I please be as specific as possible.
Thank you
You can send endEditing(_:) to the text field that is the first responder, or to any superview of it, to make it resign first responder and dismiss the keyboard. The window is a view, and is a superview of all your other views, and is easy to get a reference to:
#IBAction func calculateButtonWasTapped(sender: AnyObject?) {
view.window?.endEditing(true)
// perform calculation here
}
Let's just say that the text field is called theTextField, and when pressed the UIButton runs an #IBAction called buttonTapped().
class ViewController: UIViewController {
#IBOutlet var theTextField: UITextField!
#IBAction func buttonTapped(sender: AnyObject) {
theTextField.resignFirstResponder()
}
}

Get reference to current object

My goal is to safe a reference from the button, label or textfield inside a variable.
The problem is that I don't know on which control the user tapped.
I am having a simple application which looks like this:
The user can touch any control.
It is easy enough with just those three controls because I can drag in a action. But if I am having many of them I can't handle them all over the action methods. Is there a general way in which I can safe a reference to the control in a variable so that I can know which of the controls is the active one?
Edit
As suggested I am using a function and assigning the variable to the sender of the function. This is how it looks in code:
var currentObject: NSTextField!
override func viewDidLoad() {
super.viewDidLoad()
myTextfield.action = #selector(myAction)
}
func myAction(sender: NSTextField)
{
print("aktuell: \(sender)")
currentObject = sender
}
As you can see this only works for a NSTextfield. Is there a way in which the function works for every control?
Set the tag attribute for each item, and then you can check sender.tag to identify which object is calling it.
To set the tag, select the Attributes inspector in Storyboard (upper right side - middle button of Utilities) and look for this section:

how to avoid keyboard in textfields

I use a couple of buttons '0123456789.' (and some others) as an alternative for a keyboard. They are connected to (several) texfields. I made them all programmatically, so without storyboard. I also use UITextFieldDelegate. That works as expected, and I can input my text into the textfields.
I use the following code for my buttons:
func textFieldDidBeginEditing(textField: UITextField) {
activeField = textField
}
#IBAction func Pressed(sender: UIButton) {
if (activeField != nil) {
switch sender.tag {
case 0:
activeField!.text = activeField!.text+"0"
case 1:
The problem is that every-time I click in a textfield, the keyboard opens up too. I want to avoid that... since I made an alternative for input with the buttons. How can I get rid of the keyboard when I click in a textfield?
Just return NO from textFieldShouldBeginEditing: from the delegate you mention you are already using.
When the user performs an action that would normally initiate an editing session, the text field calls this method first to see if editing should actually proceed. In most circumstances, you would simply return YES from this method to allow editing to proceed.
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
if (textField == /* a text field that is using your custom layout */)
return NO;
}
[Source]
Though why don't you just use [textField setKeyboardType:UIKeyboardTypeNumberPad];
You should make your custom keypad the inputView of the textField. It will then be shown instead of the built-in keyboard.
i.e.:
textField.inputView = yourCustomKeypad;
I've made a similar input interface, using custom buttons...why not instead of a UITextField, use a UILabel to display the "8,900"(input).
Then just make the buttons manipulate the UILabel.text.

Disable UITextField keyboard?

I put a numeric keypad in my app for inputing numbers into a text view, but in order to input numbers I have to click on the text view. Once I do so, the regular keyboard comes up, which I don't want.
How can I disable the keyboard altogether? Any help is greatly appreciated.
The UITextField's inputView property is nil by default, which means the standard keyboard gets displayed.
If you assign it a custom input view, or just a dummy view then the keyboard will not appear, but the blinking cursor will still appear:
UIView* dummyView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1, 1)];
myTextField.inputView = dummyView; // Hide keyboard, but show blinking cursor
If you want to hide both the keyboard and the blinking cursor then use this approach:
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
return NO; // Hide both keyboard and blinking cursor.
}
For Swift 2.x, 3.x, 4.x, 5.x
textField.inputView = UIView()
does the trick
If it's a UITextField, you can set it's enabled property to NO.
If it's a UITextView, you can implement -textViewShouldBeginEditing: in its delegate to return NO, so that it'll never start editing. Or you can subclass it and override -canBecomeFirstResponder to return NO. Or you could take advantage of its editing behavior and put your numeric buttons into a view which you use as the text view's inputView. This is supposed to cause the buttons to be displayed when the text view is edited. That may or may not be what you want.
Depending on how you have your existing buttons working this could break them, but you could prevent the keyboard from showing up setting the textView's editable property to NO
myTextView.editable = NO
I have the same problem when had 2 textfields on the same view. My purpose was to show a default keyboard for one textfield and hide for second and show instead a dropdown list.
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
method simply did not work as I expected for 2 textfields , the only workaround I found was
UIView* dummyView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1, 1)];
myTextField.inputView = dummyView;
myTextField.inputAccessoryView = dummyView;
myTextField.tintColor = myTextField.backgroundColor; //to hide a blinking cursor
This will totally hide the keyboard for a target textField (DropDownList in my case) and show a default one when user switches to the 2nd textfield (Account number on my screenshot)
There is a simple hack to it. Place a empty button
(No Text) above the keyboard and have a action Event assign to it. This will stop keyboard coming up and you can perform any action you want in the handle for the button click
To disable UITextField keyboard:
Go to Main.Storyboard
Click on the UITextField to select it
Show the Attributes inspector
Uncheck the User Interaction Enabled
To disable UITextView keyboard:
Go to Main.Storyboard
Click on the UITextView to select it
Show the Attributes inspector
Uncheck the Editable Behavior
I used the keyboardWillShow Notification and textField.endEditing(true):
lazy var myTextField: UITextField = {
let textField = UITextField()
// ....
return textField
}()
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(_:)), name: UIResponder.keyboardWillShowNotification, object: nil)
}
#objc func keyboardWillShow(_ notification: Notification) {
myTextField.endEditing(true)
// if using a textView >>> myTextView.endEditing(true) <<<
}
private void TxtExpiry_EditingDidBegin(object sender, EventArgs e)
{
((UITextField)sender).ResignFirstResponder();
}
In C# this worked for me, I don't use the storyboard.
In Xcode 8.2 you can do it easily by unchecking state "enabled" option.
Click on the textField you want to be uneditable
Go to attirube inspector on right side
Uncheck "enabled" for State
Or if you want to do it via code. You can simply create an #IBOutlet of this text field, give it a name and then use this variable name in the viewDidLoad func (or any custom one if you intent to) like this (in swift 3.0.1):
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
myTextField.isEditable = false
}