Open keyboard without clicking on TextField on tvOS - Swift - swift

I'm creating an app and I need to login to it and I don't want to use 2 Text Field for insert Email and Password. What I would wish to achieve is: click on the button "Log in", then it will open the classic full screen keyboard that is usually appearing when you are doing click on UITextField in tvOS.
Can someone please write an example of code on how to invoke the keyboard clicking on a UIButton instead of UITextField? Thanks a lot!

While your UI is questionable, once your first text field finishes editing, you could start the other one.
Set the first text field UITextFieldDelegate to the view controller, and then:
class ViewController: UIViewController , UITextFieldDelegate{
#IBOutlet weak var tf2: UITextField!
#IBOutlet weak var tf1: UITextField!
#IBAction func click() {
tf1.becomeFirstResponder()
}
func textFieldDidEndEditing(_ textField: UITextField) {
if (textField == tf1){
tf2.becomeFirstResponder()
}
}
}

If you want a UITextField to open up the keyboard without the user having to tap on it, you can use yourTextField.becomeFirstResponder().
I don't think you can present the keyboard without any associated input view

Related

How to create a custom UITextField prepackaged with its own formatting behavior?

I am trying to make a reusable textfield that will format its contents to be in a currency format without relying on a containing viewcontroller to implement this behavior for it.
Right now the viewcontroller that the textfield is in is implementing the desired behavior in a TextFieldChange action and it works fine:
class MyViewController: UIViewController {
#IBOutlet weak var TextField: UITextField!
...
...
#IBAction func TextFieldChanged(_ sender: Any) {
// Format text in text field
}
}
This works, but that means I'll have to copy and paste this code into every viewcontroller that I want to have this functionality. I would like it so I could just assign the textfield its own class in the inspector so that this behavior will be a part of every textfield I make using this class. How do I do this?

Detect backspace in empty UITextField Swift Working Sample

I know that this question may sounds duplicate, but looking at the several answer, nothing is working for me yet.
Basically I have a Quizz App, a question is shown and the user needs to fill several UITextFields to answer the question (i.e. if the answer is VENICE, 6 UITextFields will be shown, 1 per letter).
It was able to detect one character in the UITextFields and once the user hits a key it will jump to the following UITextField. I use the tag of the UITextField and the method becomeFirstResponder.
The problem is that I will like to detect the backspace when a UITextField is empty so I will jump to the previous UITextField.
I have tried this solution from Jacob Caraballo (Detect backspace in empty UITextField) but I am not sure, how to use it with my existing UITextField.
For example, I have tried:
// #IBOutlet weak var textField1: UITextField!
#IBOutlet weak var textField1:MyTextField!
But calling the delegate
textField1.myDelegate = self
It crashes
I also notice, that using the Jacob's solution I won't be able to check which UITextField was used as the func textFieldDidDelete() doesn't not have an UITextField as parameter and I will need to check its tag i.e.
If textField.tag == 5 {
textField4.becomeFirstResponder()
}
Any help on this please?
If you use Jacob's solution, the deleteBackward method will be called with the current UITextField instance. So you can add a protocol to that UITextField class and pass it back to your view.
Something like this:
protocol MyTextFieldDelegate: class {
func backwardDetected(textField: MyTextField)
}
class MyTextField: UITextField {
weak var myTextFieldDelegate: MyTextFieldDelegate?
override func deleteBackward() {
super.deleteBackward()
self.myTextFieldDelegate?.backwardDetected(textField: self)
}
}

Following Apple 'Food Tracker' tutorial for Xcode - can't get button to change label text

I'm following official iOS Apps tutorial to make a basic Single View Application in Xcode.
Literally all we have done so far is:
Added a label to the UI and set initial text to 'Meal name:'
Added a textbox to the UI
Added a button to the UI
Then we've added some very simple code to the View Controller declaring the label as an outlet and a button action which, when invoked, should change the label's text to Default Text.
My ViewController code is now identical to the tutorial code namely:
class ViewController: UIViewController {
//MARK: Properties
#IBOutlet weak var nameTextField: UITextField!
#IBOutlet weak var mealNameLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
//MARK: Actions
#IBAction func setDefaultLabelText(_ sender: UIButton) {
mealNameLabel.text = "Default Text"
}
}
The problem is that when I press the button in simulator, I get an error message saying an implicitly unwrapped optional value is nil. (app launches fine, it's just when pressing the button)
As I understand it this means something is blank that can't be, but the only optionals I have are:
The textbox, which isn't blank because before I press the button I write 'biscuits' or something in it
The label text, which isn't blank because it's set to 'Meal name:' by default
I really can't work out what supposedly has a nil value that is triggering this error?
As I understand it this means something is blank that can't be
No , This means you need to make sure outlet
mealNameLabel.text = "Default Text" // here mealNameLabel is nil
is connected to the label in IB

How to get text input from UITextField?

How to get text input from UITextField ?
I tried this:
#IBAction func input(_ sender: UITextField) {
label.text = textfield.text
}
But the label doesn't get the textfield input.
Which Code should I use?
Open Main.storyboard. This is where you can see a preview of your App including your TextField.
Open the Assistant Editor. You can do so by clicking the two circles in the top right corner. Now you should have a split screen View of Main.storyboard and ViewController.swift.
Press the ctrl key and click the textfield. Now drag it in view controller.swift right below the "{" after the view controller statement at the top.
Release the key and the mouse. Now you have to select "Outlet" in the appearing and use a name
Now You can get the text anywhere in your program by calling the name you selected in step 4 and .text . E.g. name.text
What you did is create an Action. So your action function gets called whenever someone e.g. clicks the textfield. If you want to get the text of it then just use sender.text inside the action function.
I hope this helps.
Try to add #IBOutlet instead of #IBAction
For ex:
#IBOutlet weak var yourTextField: UITextField!
and then you can print it out:
print(yourTextField!)

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()
}
}