I adapted some code from a YT tutorial but the code doesn't seem to work (Xcode 10.1, Swift 4.2):
//
class ViewController: UIViewController, UITextViewDelegate {
@IBOutlet weak var myTextView: UITextView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
// Make current
// ViewController.swift
// DGTest
//
// Created by me on 15.01.19.
//
//
import UIKit
ViewController respond to UITextViewDelegate events
myTextView.delegate = self
// Update UITextView content
myTextView.text = "Some preset text..\ntap in view and then outside view.."
// Change UITextView background colour
myTextView.backgroundColor = UIColor.yellow
// User RGB colour
myTextView.backgroundColor = UIColor(red: 39/255, green: 53/255, blue: 182/255, alpha: 1)
// Update UITextView font size and colour
myTextView.font = UIFont.systemFont(ofSize: 20)
myTextView.textColor = UIColor.white
myTextView.font = UIFont.boldSystemFont(ofSize: 20)
myTextView.font = UIFont(name: "Courier", size: 17)
// Make UITextView Editable
myTextView.isEditable = true
// Capitalize all characters user types
// myTextView.autocapitalizationType = UITextAutocapitalizationType.allCharacters
// Make web links clickable
// myTextView.isSelectable = true
myTextView.isEditable = true
myTextView.dataDetectorTypes = UIDataDetectorTypes.link
// Make UITextView corners rounded
myTextView.layer.cornerRadius = 10
// Enable auto-correction and Spellcheck
// myTextView.autocorrectionType = UITextAutocorrectionType.yes
// myTextView.spellCheckingType = UITextSpellCheckingType.yes
// myTextView.autocapitalizationType = UITextAutocapitalizationType.None
// Add Tap gesture to be able to dismiss keyboard when user taps away
// You will need to declare a new function "tappedAwayFunction"
let myGesture = UITapGestureRecognizer(target: self, action: Selector(("tappedAwayFunction:")))
self.view.addGestureRecognizer(myGesture)
}
func tappedAwayFunction(sender: UITapGestureRecognizer){
myTextView.resignFirstResponder()
}
internal func textViewDidBeginEditing(_ myTextView: UITextView){
print("inside of textViewDidBeginEditing")
}
internal func textViewDidEndEditing(_ myTextView: UITextView){
print("inside of textViewDidEndEditing")
}
}
I tried @objc func and internal func as well. When tapping away the keyboard, I'm getting a backtrace:
2019-01-15 14:07:37.957426+0100 DGTest[5636:1935710] [MC] Reading from public effective user settings.
inside of textViewDidBeginEditing
2019-01-15 14:07:40.905753+0100 DGTest[5636:1935710] -[DGTest.ViewController tappedAwayFunction:]: unrecognized selector sent to instance 0x105e07150
2019-01-15 14:07:40.908931+0100 DGTest[5636:1935710] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[DGTest.ViewController tappedAwayFunction:]: unrecognized selector sent to instance 0x105e07150'
*** First throw call stack:
(0x1ad354ec4 0x1ac525a40 0x1ad26dc24 0x1da5183d8 0x1ad35a7dc 0x1ad35c48c 0x1da102f10 0x1da10b52c 0x1da108db0 0x1da108298 0x1da0fb5a0 0x1da0fb10c 0x1da0faedc 0x1da526c74 0x1da505f8c 0x1da5d4df8 0x1da5d77f0 0x1da5d02e0 0x1ad2e51f0 0x1ad2e5170 0x1ad2e4a54 0x1ad2df920 0x1ad2df1f0 0x1af558584 0x1da4eabc0 0x104d110b0 0x1acd9ebb4)
libc++abi.dylib: terminating with uncaught exception of type NSException
Changing to #selector syntax wasn't accepted by the compiler. I ended uo in letting the compiler fix the action: line to
let myGesture = UITapGestureRecognizer(target: self, action: Selector(("tappedAwayFunction:")))
self.view.addGestureRecognizer(myGesture)
But I'm left with the exception occuring.
Any clues?
--
Christoph
Edit: typo