UITapGestureRecognizer on UILabel is crashing in swift 3? - ios

Hi i want to add tap gesture on UILabel inside a UITableViewCell . here i have implemented the code like this.
override func awakeFromNib()
{
super.awakeFromNib()
let tap = UITapGestureRecognizer(target: self, action:
#selector(self.tapFunction))
label.isUserInteractionEnabled = true
label.addGestureRecognizer(tap)
}
func tapFunction(sender:UITapGestureRecognizer) {
print("tap working")
}
but it is giving me error like this
fatal error: unexpectedly found nil while unwrapping an Optional value
Can anyone tell me what is wrong in this?

This should be
let tap = UITapGestureRecognizer(target: self, action:#selector(self.tapFunction(_:)))
If you use Swift 4 add #objc
#objc func tapFunction(sender:UITapGestureRecognizer) {
print("tap working")
}

its working now with the same code . Just i deleted the cell and created a new UITableviewCell. i guess it may be xcode bug
override func awakeFromNib()
{
super.awakeFromNib()
let tap = UITapGestureRecognizer(target: self, action:
#selector(self.tapFunction))
label.isUserInteractionEnabled = true
label.addGestureRecognizer(tap)
}
func tapFunction(sender:UITapGestureRecognizer) {
print("tap working")
}

Related

How to dismiss UISearchBar keyboard without effecting other functionalities?

is how my screen looks like..
Now my code for dismissing the keyboard when the use of searchbar is over:
override func viewDidLoad() {
super.viewDidLoad()
let tap = UITapGestureRecognizer(target: self, action: #selector(handleTap))
view.addGestureRecognizer(tap)
}
#objc func handleTap() {
searchbar.resignFirstResponder()
}
By this my keyboard is getting dismissed but whenever using this, the tableview DidSelectRowAt is no more working (Though there is no error or warning). I am confused that what is the problem exactly. Please help! Thanks is advance..
Set cancelsTouchesInView to false, for detailed explanation: link
extension UIViewController {
func hideKeyboard() {
let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(UIViewController.dismissKeyboard))
tap.cancelsTouchesInView = false
view.addGestureRecognizer(tap)
}
#objc
func dismissKeyboard() {
view.endEditing(true)
}
}
Usage:
call hideKeyboard() in the viewDidLoad of the controller.

UIGesturerecognizer is not working on UIView

When I am tapping on UIView but Gesture is not working.
override func viewDidLoad() {
super.viewDidLoad()
let tap = UITapGestureRecognizer(target: self, action: #selector(self.handleTap(_:)))
viewForSHadow.isUserInteractionEnabled = true
viewForSHadow.addGestureRecognizer(tap)
// Do any additional setup after loading the view.
}
func handleTap(_sender: UITapGestureRecognizer) {
print("---------View Tapped--------")
viewForSHadow.isHidden = true
viewForAlert.isHidden = true
}
I just want to perform this action on UIView tap.
You may check in the debug view hierarchy if anything with alpha = 0 is overlapping your viewForSHadow.
You have to implement as follows:
class ViewController: UIViewController, UIGestureRecognizerDelegate {
...
override func viewDidLoad() {
super.viewDidLoad()
let tap = UITapGestureRecognizer(target: self, action: #selector(handleTap(_sender:)))
viewForSHadow.isUserInteractionEnabled = true
viewForSHadow.addGestureRecognizer(tap)
}
#objc func handleTap(_sender: UITapGestureRecognizer) {
print("---------View Tapped--------")
viewForSHadow.isHidden = true
viewForAlert.isHidden = true
}
...
}
Your code is more than enough to have working UIGestureRecognizer, you should check some other stuff like, is there something else that can consume the user interaction. And also to check if you use
isUserInteractionEnabled = false
to some parent view of viewForSHadow.
You have to use UIGestureRecognizerDelegate
let tapRecognizer = UITapGestureRecognizer(target: self, action: #selector(handleDismiss))
tapRecognizer.delegate = self
blackView.addGestureRecognizer(tapRecognizer)
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool {
return true
}
#objc func handleDismiss() {
print("handleDismiss")
}
Reference
Don't forget to set the UIGestureRecognizerDelegate in your class
You need to mention numberOfTapsRequired property of the UITapGestureRecognizer.
override func viewDidLoad() {
super.viewDidLoad()
let tap = UITapGestureRecognizer(target: self, action: #selector(handleTapGesture(_sender:)))
tap.numberOfTapsRequired = 1
viewForSHadow.isUserInteractionEnabled = true
viewForSHadow.addGestureRecognizer(tap)
}
#objc func handleTapGesture(_sender: UITapGestureRecognizer) {
print("---------View Tapped--------")
// Why are you hiding this view **viewForSHadow**
viewForSHadow.isHidden = true
viewForAlert.isHidden = true
}
Also, make sure you are not doing viewForSHadow.isUserInteractionEnabled = false anywhere in the code.
First of all you code doesn't compile. The handleTap(_:) signature must be like:
#objc func handleTap(_ sender: UITapGestureRecognizer) {
print("---------View Tapped--------")
}
Secondly, you need to first try with the minimal code in a separate project. And by minimal code I mean what you've added in the question.
class ViewController: UIViewController {
#IBOutlet weak var viewForSHadow: UIView!
override func viewDidLoad() {
super.viewDidLoad()
let tap = UITapGestureRecognizer(target: self, action: #selector(self.handleTap(_:)))
viewForSHadow.addGestureRecognizer(tap)
}
#objc func handleTap(_ sender: UITapGestureRecognizer) {
print("---------View Tapped--------")
}
}
Try with just the above code and see if you can get it working. The above code is working well at my end without any delegate or numberOfTaps.

UILabel set onClick

I'm building an application in Swift 3, so I want to call a function if I click on a particular UILabel, so I'm write this code but not works:
let tap = UITapGestureRecognizer(target: self, action: #selector(ViewController.tapFunction))
self.labelTemp.isUserInteractionEnabled = true
self.labelTemp.addGestureRecognizer(tap)
How can I render UILabel clickable ?
Set user interaction enabled for the UILabel and add the below code in the viewDidLoad()
self.label.isUserInteractionEnabled = true
let tap = UITapGestureRecognizer(target: self, action: #selector(self.labelTapped))
self.label.addGestureRecognizer(tap)
Add the tap action function as below :
#objc func labelTapped(_ gestureRecognizer: UITapGestureRecognizer) {
print("Label clicked")
}
Please make user that there is no other transparent view overlapping the UILabel in the view. If the UILabel is a part of another view then please make sure that the container View's user interaction is enabled.
Hope this helps.
Your selector should be an #objc func within self.
<#YourLabel#>.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(self.handleLabelTap)))
And when the user taps the label it will trigger:
#objc func handleLabelTap() {
// handle label tap here
}
You are lacking a function to trigger when the gesture touch is recognized. You need to add following:
let tap = UITapGestureRecognizer(target: self, action: #selector(tapFunction(_:)))
self.labelTemp.isUserInteractionEnabled = true
self.labelTemp.addGestureRecognizer(tap)
#objc func tapFunction(_ gestureRecognizer: UITapGestureRecognizer) {
// handle label tap here
}
Please ensure that You have connected the outlet to UILabel because I have created simple demo code by copy-paste your code and it is worked as expected.
override func viewDidLoad() {
super.viewDidLoad()
let tap = UITapGestureRecognizer(target: self, action: #selector(tapFunction))
self.labelTemp.isUserInteractionEnabled = true
self.labelTemp.addGestureRecognizer(tap)
}
#objc func tapFunction() {
print("tapFunction")
}
I suggest, Please remove UILabel from UIViewController and add it again.
Download sample code
Note: - Please ensure that user-interaction of UILabelis enabled
First you need to add Tap Gesture into storyboard
Create Action of that particular gesture
override func viewDidLoad() {
super.viewDidLoad()
let tapOnLabel = UITapGestureRecognizer(target: self, action: #selector(self.tapGestireAction))
self.labelTemp.isUserInteractionEnabled = true
self.labelTemp.addGestureRecognizer(tapOnLabel)
}
#IBAction func tapGestureAction(_ sender: UITapGestureRecognizer) {
//Perform action
}

#selector in gesture Recognizer in xcode9

Hii everyone i'm just shifting from xcode8 to xcode9
and when i tryed to use gesture recognizer they xcode9 is showing some error
argument of #selector refers to instate method swipe(gesture) that is not >
expose to obj c
and here my code
override func viewDidLoad() {
super.viewDidLoad()
let swipeLeft = UISwipeGestureRecognizer(target: self, action: #selector(swipe(gestuer:)))
swipeLeft.direction = .left
self.view.addGestureRecognizer(swipeLeft)
}
func swipe(gestuer: UISwipeGestureRecognizer) {
if gestuer.direction == .left {
print("this is left swipe")
}
}
so is it xcode problem of something else
You need to use the #objc attribute on swipe(gestuer:) to use it with #selector.
#objc func swipe(gestuer: UISwipeGestureRecognizer) {
if gestuer.direction == .left {
print("this is left swipe")
}
}

Swift 2.2 - Value of type ViewController has no member action

I have been getting the error "Value of type 'ViewController' has no member 'action'"
(action being the function)
Here is the code I am using for the function and Gesture Recogniser
let uilpgr = UILongPressGestureRecognizer(target: self, action: #selector(self.action))
uilpgr.minimumPressDuration = 2
Map.addGestureRecognizer(uilpgr)
func action(gestureRecogniser: UIGestureRecognizer) {
print("Gesture Recognised")
}
let uilpgr = UILongPressGestureRecognizer(target: self, action: #selector(self.action))
This is where the error occurs at self.action
What am I doing wrong and how do I fix it?
the accepted Blake Lockley's answer was right before Swift 4; however in Swift 4, the code need to be changed to the following in order to be compiled correctly (add the #objc marker to the target function):
override func viewDidLoad() {
super.viewDidLoad()
let uilpgr = UILongPressGestureRecognizer(target: self, action: #selector(ViewController.action))
}
#objc func action(gesture:UIGestureRecognizer){
print("receives gesture")
}
Your method action is declared locally within the same method that is creating the gesture recognizer.
To resolve this move your action method outside of the method it is currently in. So that is is its own method of the class ViewController and not inside any other functions.
This is working on Xcode 8:
override func viewDidLoad() {
super.viewDidLoad()
let uilpgr = UILongPressGestureRecognizer(target: self, action: #selector(ViewController.action))
}
func action(gesture:UIGestureRecognizer){
print("receives gesture")
}

Resources