Swift TextField Issue - ios

I am trying to print out the text of a UITextField to the console when a button is tapped. However, I am getting an error that says it cannot unwrap a nil. By default, I have a value typed into in and when I insert a new value, both produce this error. These IBOutlets are appropriately linked.
#IBOutlet weak var entranceFeeTextField: UITextField!
#IBAction func saveButtonTapped(sender: UIButton) {
print("The entrance fee is \(entranceFeeTextField.text)")
}
My class is only conforming to UITableViewCell and UITextFieldDelegate, and not to UIViewController. Could that be the error?

Do something like this
if let text = self.entranceFeeTextField.text {
print(text)
}

import UIKit
class ViewController: UIViewController {
#IBOutlet weak var theTextField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func onButtonPressed(sender: UIButton) {
print(self.theTextField.text)
}
}
http://i.stack.imgur.com/NXYaK.png

It Should Be Like
#IBOutlet weak var test: UITextField!
#IBAction func btn(sender: AnyObject) {
let txt = test.text!
print(txt)
}
Hope It Helps.

Related

swift 3.0 - UITextFieldDelegate protocol extension not working

I try implement UITextFieldDelegate protocol in ViewController. I have started with apple tutorial. I implemented the same such as it is on tutorial but it doesn't work. (XCODE 8)
class ViewController: UIViewController, UITextFieldDelegate {
// MARK: Properties
#IBOutlet weak var recipeNameField: UITextField!
#IBOutlet weak var recipeNameLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
recipeNameField.delegate = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: Actions
#IBAction func onSetDefaultRecipeClick(_ sender: UIButton) {
recipeNameField.text = "Deafult recipe name"
}
// MARK: UITextFieldDelegate
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textField.resignFirstResponder()
print("return")
return true
}
}
You can try to delete the first line in textField delegate.

Can't assign the variable of an Int to the UILabel

I can't assign the variable of an Int to the UILabel, how can I fix this?
import UIKit
class ViewController: UIViewController {
#IBOutlet var CoinCounterInt: UILabel!
#IBOutlet weak var CoinCounter: UILabel!
var coins = 0
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func Clicker(sender: AnyObject) {
coins += 1
CoinCounterInt = "\(coins)"
}
}
You need to assign the the string to the text property of the label:
CoinCounterInt.text = "\(coins)"

Performing segue if condition is met

You can see from the photo the set up of my UI.
The objective is when the button is pressed if a number is greater than 9 the label on the first viewcontroller to change to "number is greater than 9" and if its smaller than 9 the segue should be triggered and the label on the secondView should change to "number is greater than 9..
This is what i have done so far, but when the segue is triggered the label on the secondView does not change..
class ViewController: UIViewController {
#IBOutlet weak var labelFirst: UILabel!
#IBOutlet weak var numberEntered: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func shouldPerformSegueWithIdentifier(identifier: String!, sender: AnyObject!) -> Bool {
var number: Int? {
get{
return Int(numberEntered.text!)
}
}
if number>9 {
labelFirst.text="number is greater than 9"
return false}
else {
return true
func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
if (segue.identifier=="segue"){
let destinationVC:secondView = segue.destinationViewController as! secondView
destinationVC.outputText="number is smaller than 9"
}
}
}
}
}
the segue is connected to the button and directs to the second view.
secondView:
import UIKit
class secondView: UIViewController {
#IBOutlet weak var label: UILabel!
var outputText=String()
override func viewDidLoad() {
label.text=outputText
}
}
ui
Your prepareForSegue method is in the wrong place , it should be outside the shouldPerfomSegue method and marked with override.
The reason it is not updating on the second screen is because you are doing it inside viewDidLoad. That only happens once. Move the code to viewWillAppear.
import UIKit
class secondView: UIViewController {
#IBOutlet weak var label: UILabel!
var outputText=String()
override func viewWillAppear() {
super.viewWillAppear()
label.text = outputText
}
}

Value of type 'UIStackView' has no member 'delegate' - Swift Beginner's Guide

Good morning everyone,
I'm brand new to Swift and I'm attempting to go through the guide on Apple's website and I'm getting stuck on this page:
https://developer.apple.com/library/ios/referencelibrary/GettingStarted/DevelopiOSAppsSwift/Lesson3.html#//apple_ref/doc/uid/TP40015214-CH22-SW1
I keep getting an error that says, "Value of type 'UIStackView' has no member 'delegate'" on line 21 of my code when I add the "nameTextField.delegate = self" part. Any ideas? I had initially thought I just hadn't added the protocol but it matches the guide....
import UIKit
class ViewController: UIViewController, UITextFieldDelegate {
// MARK: Properties
#IBOutlet weak var nameTextField: UIStackView!
#IBOutlet weak var mealNameLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Handle the text field's user input through delegation callbacks
nameTextField.delegate = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: UITextFieldDelegate
func textFieldShouldReturn(textField: UITextField) -> Bool {
// Hide the keyboard
textField.resignFirstResponder()
return true
}
// MARK: Actions
#IBAction func setDefaultLabelText(sender: UIButton) {
mealNameLabel.text = "Default Test Text"
}
}
As #vadian pointed out, UITextField is definitely not the same as UIStackView.
Change this line:
#IBOutlet weak var nameTextField: UIStackView!
to this:
#IBOutlet weak var nameTextField: UITextField!
This would probably require you to reconnect the outlet.

Sending textbox values to UILabels

import UIKit
var typingSpace = 80
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBOutlet weak var typingSpace: UIView!
#IBOutlet weak var displayLabel: UILabel!
}
Basically what I'm doing is this: when I type a certain number (let's say 70) in a text box, the label will display something (eg. Your number is 70). I honestly have no idea what I'm doing. Please help.
You need 4 things
Add a outlet for your text view so you'll have #IBOutlet weak var textView: UITExtView!
Make ViewController implement UITextViewDelegate
ViewController: UIViewController, UITextViewDelegate
Make self delegate of textView on viewDidLoad:
textView.delegate = self
Implement textViewDidChange and put your logic in there
func textViewDidChange(textView: UITextView) {
// change your label text
}
Here you have be a complete class example
class ViewController: UIViewController, UITextViewDelegate {
override func viewDidLoad() {
super.viewDidLoad()
self.textView.delegate = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func textViewDidChange(textView: UITextView) {
displayLabel.text = "your text"
}
#IBOutlet weak var textView: UITextView!
#IBOutlet weak var typingSpace: UIView!
#IBOutlet weak var displayLabel: UILabel!
}
Note: #Claudio Redi has supplied the correct answer, this is another way to populate a label from a UITextField.
This is one way of accomplishing your task:
class ViewController: UIViewController {
#IBOutlet weak var typingSpace: UITextField!
#IBOutlet weak var displayLabel: UILabel!
#IBAction func handleButtonPressed(sender: UIButton) {
displayLabel.text = typingSpace.text
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}

Resources