Skip to content

Instantly share code, notes, and snippets.

@palumbo
Last active October 8, 2017 22:18
Show Gist options
  • Save palumbo/cf3a863d9a91e492dac256752a72a57c to your computer and use it in GitHub Desktop.
Save palumbo/cf3a863d9a91e492dac256752a72a57c to your computer and use it in GitHub Desktop.
//
// ViewController.swift
// Say Hello
//
// Created by Joseph Palumbo on 10/8/17.
// Copyright © 2017 Joseph Palumbo. All rights reserved.
//
import UIKit
class ViewController: UIViewController, UITextFieldDelegate {
@IBOutlet weak var myTextField: UITextField!
@IBOutlet weak var myLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
myTextField.delegate = self // connects the textfield with the delegate
}
// button code here
@IBAction func didPressButton(_ sender: UIButton) {
if let name = myTextField.text {
myLabel.text = "Hi \(name)!"
myTextField.resignFirstResponder() // hides keyboard on button press
}
}
// hides keyboard when anywhere outside
// of an element is touched
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
view.endEditing(true)
}
// tells the textfield to respond when return is pressed
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
myTextField.resignFirstResponder()
return false
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment