fatal error: unexpectedly found nil while unwrapping an Optional value (lldb) - swift

Hey so I keep getting this every time i try running the code to close the keyboard every other part works until i add these lines of code.
If anyone can help that would be great thank you!
Code starts here
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent)
{
self.view.endEditing(true)
//closes keyboard
}
func textFieldShouldReturn(textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true
}
}

override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent)
{
self.view.endEditing(true)
}
func textFieldShouldReturn(textField: UITextField) -> Bool {
if textField.text != nil
{
textField.resignFirstResponder()
}
you can also try
if textField != nil
{
textField.resignFirstResponder()
}
return true
}
}
Try this

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

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

how to override UIView.pointInside in swift?

I'm trying to port my code from obj-c to swift, but experienced a lot of troubles.
one of the issues is overriding pointInside of a UIView class:
class MyView : UIView{
func pointInside(point: CGPoint, withEvent event: UIEvent) -> Bool {
if point.x < 0 {
return false
} else {
return true
}
}}
if I don't add "override", I will get this error:
/xxx.swift:37:10: Method 'pointInside(_:withEvent:)' with Objective-C selector 'pointInside:withEvent:' conflicts with method 'pointInside(_:withEvent:)' from superclass 'UIView' with the same Objective-C selector
If I add "override", I will get this error:
/xxx.swift:37:19: Method does not override any method from its superclass
according to the doc, there should be a pointInside function
https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIView_Class/#//apple_ref/occ/instm/UIView/pointInside:withEvent:
The function in UIKit is declared as
func pointInside(_ point: CGPoint, withEvent event: UIEvent?) -> Bool
The optionality (namely UIEvent?) needs to match.
This error message seems less than useful; you might want to file a bug.
You can override with conditional check also like:
class PassThroughView: UIView {
override func pointInside(point: CGPoint, withEvent event: UIEvent?) -> Bool {
for subview in subviews as [UIView] {
if !subview.hidden && subview.alpha > 0 && subview.userInteractionEnabled && subview.pointInside(convertPoint(point, toView: subview), withEvent: event) {
return true
}
}
return false
}
}
Original post: Swift: hitTest for UIView underneath another UIView
Swift 2.2 syntax
override func pointInside(point: CGPoint, withEvent event: UIEvent?) -> Bool {
return true
}
Note UIEvent must be optional to match the superclass

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

It says method does not overide any method from its super class, what do i do? [duplicate]

Xcode 6.3. Within a class implementing UITextFieldDelegate protocol, I would like to override touchesBegan() method to possibly hide the keyboard. If I avoid a compiler error in the function spec, then there is a complier error trying to read the "touch" from the Set or NSSet, or else the super.touchesBegan(touches , withEvent:event) throws an error. One of these combinations compiled in Xcode 6.2! (So where is documentation to Swift "Set" and how to get an element from one?)
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
// Hiding the Keyboard when the User Taps the Background
if let touch = touches.anyObject() as? UITouch {
if nameTF.isFirstResponder() && touch.view != nameTF {
nameTF.resignFirstResponder();
}
}
super.touchesBegan(touches , withEvent:event)
}
Try:
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) or
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent)
Compiler error:
Overriding method with selector 'touchesBegan:withEvent:' has incompatible type '(NSSet, UIEvent) -> ()'
and
super.touchesBegan(touches , withEvent:event)
also complains
'NSSet' is not implicitly convertible to 'Set'; did you mean to use 'as' to explicitly convert?
Try:
override func touchesBegan(touches: Set<AnyObject>, withEvent event: UIEvent)
Compiler error:
Type 'AnyObject' does not conform to protocol 'Hashable'
Try:
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent)
Compiler error at
if let touch = touches.anyObject() as? UITouch
'Set' does not have a member named 'anyObject' BUT the function spec and call to super() are OK!
Try:
override func touchesBegan(touches: NSSet<AnyObject>, withEvent event: UIEvent) -> () or
override func touchesBegan(touches: NSSet<NSObject>, withEvent event: UIEvent)
Compiler error:
Cannot specialize non-generic type 'NSSet'
Swift 1.2 (Xcode 6.3) introduced a native Set type that bridges
with NSSet. This is mentioned in the Swift blog and in the
Xcode 6.3 release notes, but apparently not yet added to the official documentation (update: As Ahmad Ghadiri noted, it is documented now).
The UIResponder method is now declared as
func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent)
and you can override it like this:
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
if let touch = touches.first as? UITouch {
// ...
}
super.touchesBegan(touches , withEvent:event)
}
Update for Swift 2 (Xcode 7): (Compare Override func error in Swift 2)
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
if let touch = touches.first {
// ...
}
super.touchesBegan(touches, withEvent:event)
}
Update for Swift 3:
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
if let touch = touches.first {
// ...
}
super.touchesBegan(touches, with: event)
}
With xCode 7 and swift 2.0, use following code:
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
if let touch = touches.first{
print("\(touch)")
}
super.touchesBegan(touches, withEvent: event)
}
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
if let touch = touches.first{
print("\(touch)")
}
super.touchesEnded(touches, withEvent: event)
}
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
if let touch = touches.first{
print("\(touch)")
}
super.touchesMoved(touches, withEvent: event)
}
Using Swift 3 and Xcode 8
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
}
override func touchesCancelled(_ touches: Set<UITouch>?, with event: UIEvent?) {
// Don't forget to add "?" after Set<UITouch>
}
It is now in the Apple API reference here and for overriding in xCode version 6.3 and swift 1.2 you can use this code:
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
if let touch = touches.first as? UITouch {
// ...
}
// ...
}
The current one right now for the newest update as of xCode 7.2 Swift 2.1 on Dec 19, 2015.
Next time you get an error like this again, remove the function and start typing it again "touchesBe..." and xCode should automatically complete it to the newest one for you instead of trying to fix the old one.
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
for touch: AnyObject! in touches {
let touchLocation = touch.locationInNode(self)
//Use touchLocation for example: button.containsPoint(touchLocation) meaning the user has pressed the button.
}
}
What worked for me was:
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
if let touch = touches.first as? UITouch {
// ...
}
super.touchesBegan(touches , withEvent:event!)
}
Small addition. For swift to compile w/o error, you need to add
import UIKit.UIGestureRecognizerSubclass
Using Swift 4 and Xcode 9
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
if let touch = touches.first as? UITouch {
if touch.view == self.view{
self.dismiss(animated: true, completion: nil)
}
}
}