2

stuck for hours and still can't find the problem. I cannot add UITapGesture to two differentViews

@IBOutlet var topView: UIView
@IBOutlet var bottomView: UIView

userInteractionEnabled in topView and bottomView are enabled in storyboard

here's my code in ViewController.swift

override func viewDidLoad() {
    super.viewDidLoad()

    var tap = UITapGestureRecognizer(target: self, action: "tapTheView:")
    self.topView.addGestureRecognizer(tap)
    self.bottomView.addGestureRecognizer(tap)
}

this is the method for UITapGestureRecognizer

func tapTheView(recognizer: UITapGestureRecognizer) {

    println("Hi")

}

but when I clicked the topView nothing happened, when I clicked the bottomView my app crash and show Thread 1: EXC_BAC_ACESS in this line

class AppDelegate: UIResponder, UIApplicationDelegate {

I'm using Xcode 6 beta 2.

when I tried it in XCode 5.1.1 using the same code above (but with objective-c) it works. thanks you very much and sorry for my bad English

Shruti Thombre
  • 989
  • 4
  • 11
  • 27
yumugee
  • 993
  • 3
  • 9
  • 13
  • Are the outlets connected properly? Try "if let view = self.topView { view.addGestureRecognizer(tap); println("ok"); } else { println("no!");}" etc. – Grimxn Jun 26 '14 at 13:35
  • hi, thanks for your time to reply. I've checked many times and yes topView and bottomView connected to the scene in storyboard. I've tried your code but still got some problem. – yumugee Jun 26 '14 at 13:41

1 Answers1

2

This works without crash in Xcode beta 2. One thing is you have to add separate gestures for each view Check this link. i.e

var tap = UITapGestureRecognizer(target: self, action: "tapTheView:")
self.topView.addGestureRecognizer(tap)

var tap2 = UITapGestureRecognizer(target: self, action: "tapTheView:")
self.bottomView.addGestureRecognizer(tap2)
Community
  • 1
  • 1
Yatheesha
  • 10,412
  • 5
  • 42
  • 45
  • hi thank you very much. I didn't know if I can't add 1 instance of UITapGestureRecognizer to two differents view. it works if I made two different instance of UITapGestureRecognizer for each views – yumugee Jun 26 '14 at 14:49