In iOS 10.0 beta 4 for iPhone (XCode 8 beta 5), when the user taps a number pad text field or a decimal pad text field, the system's number or decimal pad is presented, instead of the one belonging to the keyboard extension. At least that's true with my keyboard extension, and with the skeleton custom keyboard extension provided by Xcode.
Also, when the user taps the Number Pad text field or the Decimal Pad text field in my test program, the console displays this message (this is the version for the number pad, which is type 4):
2016-08-11 21:58:43.007 TestNumberPad[34090:1780242] Can't find keyplane that supports type 4 for keyboard iPhone-PortraitChoco-NumberPad; using 1144316255_PortraitChoco_iPhone-Simple-Pad_Default
Here are the results of my test program. The default keyboard extension should appear in all 3 cases.
Here are the the top part of the attribute inspector entries for my 3 text fields
Here's ViewController.swift
import UIKit
class ViewController: UIViewController, UITextFieldDelegate {
#IBOutlet weak var defaultField: UITextField! // 'tag' property = 3
#IBOutlet weak var numberField: UITextField! // 'tag' property = 4
#IBOutlet weak var decimalField: UITextField! // 'tag' property = 5
override func viewDidLoad() {
super.viewDidLoad()
defaultField.delegate = self
numberField.delegate = self
decimalField.delegate = self
// I TRIED AN ALTERNATE TEST, WHERE I SPECIFY THE KEYBOARD TYPE PROGRAMMATICALLY, NOT IN THE IB:
//
// 1. IN THE INTERFACE BUILDER, SET ALL 3 KEYBOARD TYPES TO 'DEFAULT'.
// 2. UNCOMMENT THE CODE BELOW
//
// UNFORTUNATELY, THE RESULTS ARE THE SAME
//
// defaultField.keyboardType = UIKeyboardType.default
// numberField.keyboardType = UIKeyboardType.numberPad
// decimalField.keyboardType = UIKeyboardType.decimalPad
}
func textFieldDidBeginEditing(_ textField: UITextField) {
switch textField.tag {
case 3: print("Tapped default field")
case 4: print("Tapped number pad")
case 5: print("Tapped decimal pad")
default:
print("Text field doesn't have a tag!!")
}
}
}
Finally, here's the console output when tapping, in order, the default field, the number pad field, and the decimal pad field.
Tapped default field 2016-08-12 11:13:08.561083
TestNumberPad[2201:221335] [MC] System group container for
systemgroup.com.apple.configurationprofiles path is
/private/var/containers/Shared/SystemGroup/systemgroup.com.apple.configurationprofiles
2016-08-12 11:13:08.563097 TestNumberPad[2201:221335] [MC] Reading
from public effective user settings.
2016-08-12 11:13:08.799888 TestNumberPad[2201:221335] [App] if we're in the real pre-commit
handler we can't actually add any new fences due to CA restriction
Tapped number pad
2016-08-12 11:13:15.070750 TestNumberPad[2201:221335] Can't find keyplane that supports type 4
for keyboard iPhone-Portrait-NumberPad; using
160517473_Portrait_iPhone-Simple-Pad_Default
Tapped decimal pad
2016-08-12 11:13:17.627520 TestNumberPad[2201:221335] Can't find
keyplane that supports type 8 for keyboard iPhone-Portrait-DecimalPad;
using 405786210_Portrait_iPhone-Simple-Pad_Default
The App Store Review Guidelines still require custom keyboards to furnish number pads and decimal pads, so this seems an important issue.
I guess you were testing on the simulator so when the keyboard is not there it gives this error in logs.
So just toggle keyboard, or use the device.
Related
I have a textfield that accepts numbers.
When tapping outside the area of the number pad to dismiss the keyboard it passes the text field value to a function.
When the text field is empty I get the NIL unpacked optional error and it crashes.
I am trying to find a way to pass a "0" to the variable when the text field has nothing in it.
The variable
#IBOUTLET weak var per1kCollector: UITextField?
var per1k = 0
The handler to close the keyboard and call the function
per1k grabs the textfield
per1k needs a 0 if collector.text is NIL
#objc func handleTap(_ recognizer: UITapGestureRecognizer) {
print("closing number pad")
view.endEditing(true)
per1k = (Int(per1kCollector!.text!)! ?? 0)
doSomeMath()
}
This keeps stating "cannot convert value of type 'int' to expected argument type 'string'. But, I'm taking the Int of the text field, and per1k IS type int. ????
Any time your code says ! you should expect to crash. Try to avoid it. There's very little need for it. In this case just use a question mark:
per1k = Int(per1kCollector?.text ?? "0") ?? 0
So essentially I'm creating a calculator type app, in which I only want to intake numbers, decimals, and the negative sign. I'm fairly new to Xcode and swift so I wouldn't consider my knowledge extensive when it comes to knowing how to handle these sorts of issues.
Here is my current attempt at a solution
let v = Double(inputOne.text!)!
(inputOne. is the associated text field)
This works well in my algorithm when everything is inputted as it should be, however when inputting anything other than a valid double I receive "Fatal error: Unexpectedly found nil while unwrapping an Optional value", and the app crashes.
I figured someone may have already come up with a creative solution to this problem or that there may be some command in Xcode that limits text fields in this way, however I am new to Xcode and unaware of such things if they exist. Maybe there is some way I can implement an error message that occurs whenever something other than a valid double is inputted without crashing the app? Any creative solutions would be appreciated.
You can use a UITextView as your input field (if you want a one-line input, use a textField). Then, simply set your ViewController as the textView's delegate -inside your ViewController add the following line (and don't forget to inherit UITextViewDelegate):
textView.delegate = self
Essentially, what this does is allow the ViewController to take ownership of some of the textView's functionalities. In this case, we are interested in filtering the user input and for that you can use the following function:
/// Intercept whatever character the user is typing on the keyboard
func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
// If the user presses "enter", handle it the way you want
guard text.rangeOfCharacter(from: CharacterSet.newlines) == nil else {
// Handle new line
}
// If character is a digit, a dot, or a dash, allow input, else reject it.
if text == "-" || text.rangeOfCharacter(from: CharacterSet.decimalDigits) != nil || text == "." {
return true
} else {
return false
}
}
So I have 4 textfields with lines underneath them all. When the user enters a number into the first textfield it jumps to the next and so on until a number it entered into the last field at that point the keyboard will just resign and the pin will be evaluated. I want to be able to change the color of the lines depending on which textfield is focused the time.
What I have tried
So far I've tried making an instance of an observable object in a representable swift file that makes up my textfield. So I'll change the Observable Object when nextField becomes first responder and I'll change a state variable in my SwiftUI file. When I try to make an instance of my Observable Object in my representable file I get an error that states "Class 'SchoolCode_WrappableTextField' has no initializers". Overall I'm essentially just trying to change the color of the underline depending on which textfield is focused at the time. Any help is appreciated, thanks.
#objc func textChanged(sender: UITextField) {
if (sender.text?.count)! > 0 {
if let nextField = sender.superview?.superview?.viewWithTag(sender.tag + 1) as? UITextField{
nextField.becomeFirstResponder()
}else{
sender.resignFirstResponder()
}
}
}`
UI
I have two localizations in my app (English and Arabic). I have set Keyboard type for many of UITextFields to decimalPad. I need to enforce English only decimal pad for these fields. But when the app language is in Arabic, the keypad is shown in Arabic. It works fine till iOS 12 by subclassing UITextField and override textInputMode. Here is the code:
override var textInputMode: UITextInputMode? {
for mode in UITextInputMode.activeInputModes {
if mode.primaryLanguage?.containsSubString(subString: "en") {
return mode
}
}
return nil
}
In iOS 13 the above code has no effect. I followed Showing the system Emoji keyboard by default on iOS 13. Override textInputContextIdentifier but still no success.
override var textInputContextIdentifier: String? { return "en-US" }
PS: It works fine for other keyboard types - Number, Asci etc. Issue occurs for decimal pad only.
This is my very first question on Stack Overflow so I hope I'm following the rules correctly.
I'm making a To-Do list app in Swift and I'm trying to prevent blank or empty spaces from being appended to the table.
My UITextField:
#IBOutlet weak var item: UITextField!
My addItem button:
#IBAction func addItem(sender: AnyObject) {
if item.text.isEmpty {
//Do not append item to array
}
Thank you for any help!
You can do this
if item.text.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceCharacterSet()) != "" {
//addNew()
}
stringByTrimmingCharactersInSet : Returns a new string made by
removing from both ends of the receiver characters contained in a
given character set. The parameter NSCharacterSet.whitespaceCharacterSet removes the whitespace from both ends.
let trimmedString = item.text.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceCharacterSet())
if countElements(trimmedString) > 0 {
// append the item
}
The more thorough solution might be to use the UItextFieldDelegate protocol and prevent people from typing blank spaces in the first place. Hard to do camel casing on mobile, so I apologize for typos.