Swift method doesn't override any method from its superclass - iphone

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 {
}

Related

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.

Why does double tap gesture only work once on UITextView?

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
}
}

Swift - view.endEditing in touchesBegan not working

I try to hide keyboard by pressing on any place of the screen. I use following code
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
self.view.endEditing(true)
}
But id doens't work, keyboard is not hiding.
How to use it correctly ?
Conform to UITextFieldDelegate
In your viewController() assign delegate of YOURTextField.delegate = self.
In your method use self.YOURTextField.resignFirstResponder()

Method conflicts with Method from Superclass - Override gives error [duplicate]

This question already has answers here:
Overriding method with selector 'touchesBegan:withEvent:' has incompatible type '(NSSet, UIEvent) -> ()'
(9 answers)
Closed 7 years ago.
I've got the class Menu: SKScene, then a func touchesBegan inside of it which gives me the error of:
Method 'touchesBegan(:withEvent:)' with Objective-C selector 'touchesBegan:withEvent:' conflicts with method 'touchesBegan(:withEvent:)' from superclass 'UIResponder' with the same Objective-C selector
Anyway, if I add override in front of the function it says:
Method does not override any method from its superclass.
Any idea? The whole code:
import SpriteKit
class Menu: SKScene {
var title : SKLabelNode?
var start : SKLabelNode?
override func didMoveToView(view: SKView) {
backgroundColor = SKColor.whiteColor()
self.title = SKLabelNode(fontNamed: "Chalkduster")
self.start = SKLabelNode(fontNamed: "Chalkduster")
self.title!.name = "title"
self.start!.name = "new"
self.addChild(self.title!)
self.addChild(self.start!)
}
func touchesBegan(touches: NSSet, withEvent even: UIEvent) {
self.menuHelper(touches)
}
func menuHelper(touches: NSSet) {
for touch in touches {
let nodeAtTouch = self.nodeAtPoint(touch.locationInNode(self))
if nodeAtTouch.name == "title" {
print("Title pressed")
}
else if nodeAtTouch.name == "new" {
print("Start pressed")
}
}
}
The signature (from the docs) for this method is...
func touchesBegan(_ touches: Set<UITouch>, withEvent event: UIEvent?)
touches is a Set (not NSSet) of UITouch objects and event is an optional UIEvent.
You also need to override it...
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
self.menuHelper(touches)
}

Swift 1.2: Method does not override any method from its superclass

In my SKScene subclass I have implemented a touchesBegan method. This method had the NSSet changed to Set in order to make it Swift 1.2 compatible (see this question).
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
// ...
}
Now the compiler gives me an error: Method does not override any method from its superclass. My code -as any Swift code- was broken in 1.2, and I have fixed every issue except this override case. Am I missing something here?
This worked for me
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
//...
}
For more information about why is happening this error you can read from this answer that explain in detail about this: https://stackoverflow.com/a/30892467/2091181
So the "problem" had everything to do with the fact that the project had a Set class in it implemented, and I did not detect this redundancy. Quite a silly mistake.