How to get text input from UITextField? - swift

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!)

Related

Open keyboard without clicking on TextField on tvOS - 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

How do you get a text field input in you view controller code?

I’m trying to make Xcode print "Nice!" when you type in "Hi". I've used a IBOutlet, but I don’t know how to use the user input in my code. Also BTW I'm using Storyboard and not SwiftUI. It also gives me an error when I try to compare the datatype UIViewController and a String. Here is my view controller code(with the default App Delegate and Scene Delegate code):
import UIKit
class ViewController: UIViewController {
#IBOutlet var yeet: [UITextField]!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
func fuel(_ yeet:UIViewController) -> Int {
if yeet == ("hi") {
print("Nice!")
}
}
}
your textfield show be setup as
#IBOutlet weak var textFeildName: UITextField!
you will need to change a couple things inside of your file to prevent a crash. I'd delete the textfield and drag it into the assistant view and give it a new name.
but before you press "connect" press the "outlet" tab and change it to "Action" and then a new selector should come up select "Editing Did End" and go to the top and press "Did End On Exit"
after that is done would want to reference the variable of the text field:
example:
#IBAction func TextFieldName(_ sender: Any) {
if(self.TextFeildName.text!.contains("hi")){
print("Nice!")
}
}
On top of all this, you do not compare strings with == that's only if you compare 2 separate strings for example stringOne == stringTwo if you are comparing or asking if a string contains anything you'd want to use the developing language specific string container IE: .contains
Also, please do not include "Xcode" as a tag with your question, as that should be reserved for Xcode related problems. not Swift or objective-c coding issues.

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 programmatically set an NSPopUp title. Swift4 OSX

I have an IB NSPopUpButton with a list of items and a number of NSTextFields.
The items from the popup are used to populate the text fields (ingredients) until a recipe is complete.
I would like to reset the popup title after each item is used but cannot find the code and syntax to do this.
I'd also like to be able to click on any of the text fields after selecting an ingredient to drop it there. Drag and drop from the popup would be ideal but I cannot find a simple way to do either so am currently using another button adjacent to each text field to initiate the drop.
This is neither elegant nor ideal.
Hopefully someone can propose better solutions.
My code currently looks like this
var ItemLabel: String = ""
#IBAction func Ingredients(_ sender: NSPopUpButton){
ItemLabel = sender.titleOfSelectedItem ?? "Nil"
}
#IBOutlet weak var Ingredient1: NSTextField!
#IBAction func AddIngredient1(_ sender: NSButton){ // button next to text field
Ingredient1Label.stringValue = ItemLabel
// Here I need to reset the popup title
}

How can I get the contents of the text box below and display the retrieved text on the label when I press the button?

enter image description here
Please tell me what kind of code to write
I suppose you've already created the reference for the textbox as TextView. Now you need to create a new IBOutlet for the label. Once you have the reference to the label you can get the text value from the textview and show it in the label.
#IBAction func display(_sender) {
self.labelView.text = self.TextView.text
//if you want to clear the textview
self.TextView.text = ""
}
Create outlet for your label
#IBOutlet weak var yourlabel: UILabel!
On Button Action right code to set the value to label.
#IBAction func display(_sender) {
yourlabel.text = self.TextView.text
}