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?
Related
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.
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.
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)"
I read here that you have to use pickerView.selectRow inside viewDidAppear(). While pickerView.selectRow is working inside func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) it does not work in viewDidAppear().
I just want to set the initial row to another row than 0
What am I missing here?
import UIKit
class FirstViewController: UIViewController, UIPickerViewDelegate {
let scrollerOfset = 100
var kiloMeters = [Int](-100...200)
#IBOutlet weak var pickerActive: UILabel!
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 viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
pickerView.selectRow(scrollerOfset, inComponent: 0, animated: false)
}
}
Xcode gives this error
Cannot invoke 'selectRow' with an argument list of type '(Int, inComponent: Int, animated: Bool)'
at line 32 (in viewDidAppear(animated: Bool))
Update
Code changed to this to make it work:
import UIKit
class FirstViewController: UIViewController, UIPickerViewDelegate {
let scrollerOfset = 100
var kiloMeters = [Int](-100...200)
#IBOutlet weak var pickerActive: UILabel!
#IBOutlet weak var picker: UIPickerView!
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 viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
picker.selectRow(scrollerOfset, inComponent: 0, animated: false)
}
}
Seems you are missing the declaration of pickerView property in your class, try declare it with under pickerActive, like
#IBOutlet weak var pickerActive: UILabel!
#IBOutlet weak var picker: UIPickerView!
Then connect it thru IB like pickerActive
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.
}
}