Alex Kristensen Alex Kristensen - 1 month ago 14
iOS Question

How to resign keyboard in iOS

I have made a small program which I use to count the money in the safe at work, but after updating my iPhone, it didn't work anymore.

After a lot of reading and so on I fixed everything, but I haven't found a new way to resign the keyboard.

Before the update I used this:

@IBAction func resignKeyboard(_sender: AnyObject) {
_sender.resignFirstResponder()
}


This doesn't work anymore so I would love a new way of doing it. I have searched quite a bit for a solution, but I haven't understood any of the solutions I have found. So please simplify your answers as a lot as possible.

This is everything from the "ViewController.Swift" from my program:

//
// ViewController.swift
// Pengeskabstæller
//
// Created by Alex on 09/07/2016.
// Copyright © 2016 Alex. All rights reserved.
//

import UIKit


class ViewController: UIViewController, UITextFieldDelegate {

@IBOutlet weak var In50B: UITextField!
@IBOutlet weak var In50L: UITextField!
@IBOutlet weak var In20B: UITextField!
@IBOutlet weak var In20L: UITextField!
@IBOutlet weak var In10B: UITextField!
@IBOutlet weak var In10L: UITextField!
@IBOutlet weak var In5B: UITextField!
@IBOutlet weak var In5L: UITextField!
@IBOutlet weak var In2B: UITextField!
@IBOutlet weak var In2L: UITextField!
@IBOutlet weak var In1B: UITextField!
@IBOutlet weak var In1L: UITextField!
@IBOutlet weak var In05B: UITextField!
@IBOutlet weak var In05L: UITextField!
@IBOutlet weak var Ialt: UILabel!


@IBAction func resignKeyboard(_sender: AnyObject) {
_sender.resignFirstResponder()
}
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 Knap(sender: AnyObject) {
//Hvad knappen gør

//Ganger input ved værdi af bundt eller løse
var x50B: Int = Int(In50B.text!)!
x50B = x50B*5000
var x50L:Int = Int(In50L.text!)!
x50L = x50L*500
var x20B:Int = Int(In20B.text!)!
x20B = x20B*4000
var x20L:Int = Int(In20L.text!)!
x20L = x20L*400
var x10B:Int = Int(In10B.text!)!
x10B = x10B*2000
var x10L:Int = Int(In10L.text!)!
x10L = x10L*200
var x5B:Int = Int(In5B.text!)!
x5B = x5B*1000
var x5L:Int = Int(In5L.text!)!
x5L = x5L*200
var x2B:Int = Int(In2B.text!)!
x2B = x2B*500
var x2L:Int = Int(In2L.text!)!
x2L = x2L*50
var x1B:Int = Int(In1B.text!)!
x1B = x1B*500
var x1L:Int = Int(In1L.text!)!
x1L = x1L*50
var x05B:Int = Int(In05B.text!)!
x05B = x05B*200
var x05L:Int = Int(In05L.text!)!
x05L = x05L*20

//Lægger det hele sammen

let penge1 = (x50B + x50L + x20B)
let penge2 = (x20L + x10B + x10L)
let penge3 = (x5B + x5L + x2B + x2L)
let penge4 = (x1B + x1L + x05B + x05L)
let penge99 = String(penge1+penge2+penge3+penge4)

//Printer ialt
Ialt.text = penge99
}

}

Answer

I think this is a better way to do it, instead of adding an outlet.

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    view.endEditing(true)
}

Hope it helps!