swift 3.0 - UITextFieldDelegate protocol extension not working - ios

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.

Related

does not implement methodSignatureForSelector Error: Keyboard Notifications

I am trying to handle the keyboard notifications using a separate class.
Class-KeyboardHandling:
class KeyboardHandling{
class func addObserverKeyboardWillShow(){
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(notification:)), name: .UIKeyboardWillShow, object: nil)
}
#objc func keyboardWillShow(notification: NSNotification){
let duration = notification.userInfo![UIKeyboardAnimationDurationUserInfoKey] as! Double
print(duration)
}
}
MainVC:
class MainVC: UIViewController {
//MARK: Outlets
#IBOutlet weak var textInput: UITextField!
#IBOutlet weak var bottomToolbar: UIToolbar!
#IBOutlet weak var toolbarBottonConstraint: NSLayoutConstraint!
//MARK: Variables
var toolBarIntialBottomConstraint: CGFloat!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func viewDidAppear(_ animated: Bool) {
toolBarIntialBottomConstraint = toolbarBottonConstraint.constant
KeyboardHandling.addObserverKeyboardWillShow()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
The implementation works perfectly when addObserverKeyboardWillShow() is not a class function.
I get the error otherwise
Error Screenshot (Xcode 8.0):
Can someone explain why the same?

Swift TextField Issue

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.

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

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