Why does double tap gesture only work once on UITextView? - swift

I have a textview that I want to allow the user to edit when they double tap. The double tap works fine when the viewcontroller first loads. After I edit the textview and close the textview then reopens for editing with just a single tap. My code is:
import UIKit
class ViewController: UIViewController, UITextViewDelegate {
#IBOutlet weak var textView: UITextView!
let myTaps = UITapGestureRecognizer()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
textView.gestureRecognizers = nil
myTaps.addTarget(self, action: "handleTaps:")
myTaps.numberOfTapsRequired = 2
myTaps.numberOfTouchesRequired = 1
textView.addGestureRecognizer(myTaps)
}
func handleTaps(recognizer: UITapGestureRecognizer) {
print("Taps")
textView.becomeFirstResponder()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
view.endEditing(true)
super.touchesBegan(touches as Set<UITouch>, withEvent: event)
}
func textViewShouldEndEditing(textView: UITextView) -> Bool {
textView.endEditing(true)
return true
}
}

Related

textFieldShouldReturn func not working on scrollView

How could i make the text disappeared when the user tap the screen when UIscrollview is active
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
self.view.endEditing(true)
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true
}
I'm using the typical func for get and dismiss the keyboard
I try to
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
UIScrollView.endEditing(true)
}
This in viewdidLoad ()
let scrollViewTap = UITapGestureRecognizer(target: self, action: #selector(self.scrollViewTapped))
scrollViewTap.numberOfTapsRequired = 1
scroll_view.addGestureRecognizer(scrollViewTap)
and This in class
#objc func scrollViewTapped() {
scroll_view.endEditing(true)
self.view.endEditing(true) // anyone
}
in ViewDidLoad
let recognizer = UITapGestureRecognizer(target: self, action: #selector(self.touch))
recognizer.numberOfTapsRequired = 1
recognizer.numberOfTouchesRequired = 1
elskrolll.addGestureRecognizer(recognizer)
elskroll must be your UIScrollView name:
outside the ViewDidLoad
#objc func touch() {
//print("Touches")
self.view.endEditing(true)
}

Issue with textview in swift 3

I am working in a form in swift 3, my problem is when I tried to hide the keyboard in textview because the keyboard reuse to hide and is still visible in the previous VC.
My question is what I missing to fix this and when tap outside of textview the keyboard hide automatically?
Here the code:
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
self.view.endEditing(true)
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textField.resignFirstResponder()
return(true)
}
func textFieldDidBeginEditing(_ textField: UITextField) {
if(textField == names){
scrooView.setContentOffset(CGPoint(x:0, y:250), animated: true)
}
else if(textField == email){
scrooView.setContentOffset(CGPoint(x:0, y:250), animated: true)
}
else if (textField == tel){
scrooView.setContentOffset(CGPoint(x:0, y:250), animated: true)
}
else if (textField == message){
scrooView.setContentOffset(CGPoint(x:0, y:450), animated: true)
}
}
func textFieldDidEndEditing(_ textField: UITextField, reason:
UITextFieldDidEndEditingReason) {
scrooView.setContentOffset(CGPoint(x:0, y:0), animated: true)
}
extension UIViewController{
func hideKeyboard(){
let tap: UITapGestureRecognizer = UITapGestureRecognizer(
target: self,
action: #selector(UIViewController.dismissKeyboard))
view.addGestureRecognizer(tap)
}
#objc func dismissKeyboard(){
view.endEditing(true)
}
}
Here a link with the issue!
You are using a scrollView, try to add the GestureRecognizer in them, like this:
class ViewController: UIViewController {
#IBOutlet weak var scrooView: UIScrollView!
#IBOutlet weak var tfTest: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
scrooView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(dismissKeyboard)))
// 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.
}
#objc func dismissKeyboard(){
tfTest.endEditing(true)
}
})
If you add your Gesture in your View, it could never actived 'cause you are touching the scrollview that is over the view.

Overriding methods in a class extension constrained to a protocol in swift

I am trying to add default implementations to UIViewController touches began to all controllers conforming to a protocol through a protocol extension. There the touch would be sent to a custom view all controllers implementing this protocol have.
Here's the initial state:
protocol WithView {
var insideView: UIView! { get }
}
class Controller1: UIViewController, WithView {
var insideView: UIView!
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
insideView.touchesBegan(touches, with: event)
}
/* Functionality of Controller 1 */
}
class Controller2: UIViewController, WithView {
var insideView: UIView!
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
insideView.touchesBegan(touches, with: event)
}
/* Functionality of Controller 2 */
}
What I'd like to accomplish is a situation where all the UIViewControllers forwarded the touches to the insideView without specifying so for every controller the same way. Something like this:
protocol WithView {
var insideView: UIView! { get }
}
extension UIViewController where Self: WithView {
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
insideView.touchesBegan(touches, with: event)
}
}
class Controller1: UIViewController, WithView {
var insideView: UIView!
/* Functionality of Controller 1 */
}
class Controller2: UIViewController, WithView {
var insideView: UIView!
/* Functionality of Controller 2 */
}
But this does not compile, saying 'Trailing where clause for extension of non-generic type UIViewController'
I tried to define it the other way around, like so:
extension WithView where Self: UIViewController {
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
insideView.touchesBegan(touches, with: event)
}
}
and while the extension is properly formatted, the compiler complains, as it cannot 'override' things in a protocol extension.
What I'd like is a class extension constrained to a protocol, such as I can override this methods and not being forced to copy-paste code inside all my controllers implementing this protocol.
Edit: as per proposed solutions
I also came up with this solution:
protocol WithView {
var insideView: UIView! { get }
}
extension UIViewController {
override open func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
guard let viewSelf = (self as? WithView) else {
super.touchesBegan(touches, with: event)
return
}
viewSelf.insideView.touchesBegan(touches, with: event)
}
}
class Controller1: UIViewController, WithView {
var insideView: UIView!
/* Functionality of Controller 1 */
}
class Controller2: UIViewController, WithView {
var insideView: UIView!
/* Functionality of Controller 2 */
}
It does what I want, but it feels a bit messy though, because then all the UIViewControllers would intherit this behavior, and would override its code, checking if they implement the protocol.
You can define your own superclass for all view controllers and check if self conforms to the particular protocol (WithView in your case) to decide if you should forward touch events to any other view.
class MyViewController: UIViewController {
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
if let selfWithView = self as? WithView {
selfWithView.insideView.touchesBegan(touches, with: event)
} else {
super.touchesBegan(touches, with: event)
}
}
}
This is more flexible approach, you don't have to store insideView property in every view controller subclass.
You could do this by creating a class and sub-classing from it:
class WithViewController: UIViewController, WithView {
var insideView: UIView!
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
insideView.touchesBegan(touches, with: event)
}
}
class ViewController: WithViewController {
}
The only downside to this is you have to have a default insideView and it never get's changed.

Swift method doesn't override any method from its superclass

I am relatively new to Swift and I am following some basic tutorials but I seem to be having a problem with some methods which attempt to allow the user to press return to minimise the keyboard or to click off the keyboard and the keyboard will disappear, I understand why I am receiving these errors but have no clue how to go about fixing it, I feel something may have been changed in the newer version of Swift I am using as he is using an older version than me, could anyone possibly explain how to go about fixing these two errors please? Any help would be greatly appreciated here is my source code: (First error, value of type 'viewController' has no member 'text' and secondly, touchesBegan method does not override any method from its superclass)
import UIKit
class ViewController: UIViewController {
#IBAction func buttonPressed(sender: AnyObject) {
label.text = textArea.text
}
#IBOutlet weak var textArea: UITextField!
#IBOutlet weak var label: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.text.delegate = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
self.view.endEditing(true)
}
func textFieldShouldReturn(textField: UITextField!) -> Bool {
textField.resignFirstResponder()
return true
}
}
You have 2 problems here, based on the images you posted:
1) The method touhesBegan you are using is not correct:
Correct one:
func touchesBegan(_ touches: Set<UITouch>, withEvent event: UIEvent?)
Yours:
func touchesBegan(touches: NSSet, withEvent event: UIEvent)
I think you want a delegate for the UITextField, so this one is not corerct: touchesBegan is a method for the UIReponder delegate and not for UITextFieldDelegate.
Here you can find the reference for the UITextFieldDelegate.
2) the variable text doesn't exists in your code. I think you wanted to use textArea instead.
Hope this can help you, happy coding!
In your case change following thing:
instead of :
self.text.delegate = self
change :
self.textArea.delegate = self
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
}
And for delegate add like this
class ViewController: UIViewController, UITextFieldDelegate {
}

UITextView keyboard is taking 2 taps to open.

I have a UITextView with a label over it as a placeholder. When the user taps on the UITextView the label disappears but for the keyboard to appear it takes another 2 taps. When I remove the tap gesture that hides the label the keyboard works perfectly. Here is my code any ideas as to what the problem is???
var tapTerm:UITapGestureRecognizer = UITapGestureRecognizer()
override func viewDidLoad() {
super.viewDidLoad()
bioTextfield.delegate = self
tapTerm = UITapGestureRecognizer(target: self, action: "tapTextView:")
// bioPlaceholderLabel.addGestureRecognizer(tapTerm)
bioTextfield.addGestureRecognizer(tapTerm)
}
func tapTextView(sender:UITapGestureRecognizer) {
// hide placeholder label text
bioPlaceholderLabel.text = ""
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
// when user touches outside the keyboard close the keyboard
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
self.view.endEditing(true)
}
// when user presses the return button close the keyboard
func textView(textView: UITextView, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool {
if text == "\n" {
textView.resignFirstResponder()
return false
}
return true
}
Any help would be greatly appreciated!
Easier way, you should use this custom UITextView (with place holder): KMPlaceholderTextView. I'm using, so great.