1

Am trying to show a UIPickerView thats already hidden using UITapGestureRecognizer. Am trying to add this GestureRecognizer to a UILabel.

The UITapGestureRecognizer doesn't work on taping the UILabel once. Any pointers appreciated. Thanks

class FirstViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource  {

    @IBOutlet var baseCurrency: UITextField!
    @IBOutlet var excahngeCurrency: UITextField!
    @IBOutlet var dropDown: UIPickerView!
    @IBOutlet var currencyLabel: UILabel!

    var list: [String: AnyObject] = [String: AnyObject]()
    var titles: [String] = []

    override func viewDidLoad() {
        super.viewDidLoad()

        currencyLabel.isUserInteractionEnabled = true
        let tap:UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(showpickerView))
        tap.numberOfTapsRequired = 1
        currencyLabel.addGestureRecognizer(tap)

        let url:URL = URL(string: "https://openexchangerates.org/api/currencies.json?app_id=7486951aeb994599a08cd7979bef3c03")!
        Alamofire.request(url).responseJSON(completionHandler: {
            response in

            switch response.result {

            case.success:

                self.list = response.result.value as! [String: AnyObject]

                for (key, _) in self.list{

               self.titles += [key]

                }

                print(self.titles)

                if (self.titles.count > 0) {

                    self.dropDown.reloadAllComponents()

                    print("test")

                } else {
                    print("there is no data for your picker view")
                }



            case .failure:
                print("error")


            }


        })

    }

    func showpickerView(_ sender:UITapGestureRecognizer){

      print("test")

        //self.dropDown.isHidden = false

    }

    func numberOfComponents(in pickerView: UIPickerView) -> Int
    {
        return 1
    }

    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String?{


        return self.titles[row]


    }

    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int
    {
        return self.titles.count
    }



    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int){


        currencyLabel.text = self.titles[row]
        self.dropDown.isHidden = true


    }




    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}
Lucius
  • 2,794
  • 4
  • 20
  • 42
Phani.m
  • 29
  • 4

5 Answers5

0

After you assign tap a value, add tap.delegate = self.

ClayJ
  • 377
  • 2
  • 14
0

try This code after in this line ( tap.numberOfTapsRequired = 1)

tap.cancelsTouchesInView = false
Vignesh J
  • 257
  • 1
  • 2
  • 14
  • Thanks Vignesh , i ve added tap.cancelsTouchesInView = false after tap.numberOfTapsRequired = 1. It still doesn't work. – Phani.m Mar 24 '17 at 14:19
  • Removed the line tap.numberOfTapsRequired = 1 in your code. – Vignesh J Mar 25 '17 at 06:45
  • tried this , used only tap.cancelsTouchesInView = false and removed tap.numberOfTapsRequired = 1. Still doesn't print "test" when i click on the label – Phani.m Mar 25 '17 at 20:41
0

You will have to add @objcattribute to your showpickerView(_ sender:UITapGestureRecognizer) method to use it with #selector.

Refer this for more details '#selector' refers to a method that is not exposed to Objective-C

Community
  • 1
  • 1
amiladiman
  • 586
  • 5
  • 15
  • hi , am using swift, adding , but still to check i have added @objc. I have an error in appdelegate – Phani.m Mar 24 '17 at 14:16
  • looks like the error is gone when i build it . I have added @objc func showpickerView(_ sender:UIGestureRecognizer) it still doesn't print test – Phani.m Mar 25 '17 at 20:38
0

First , you fix function like this:

func showpickerView(_ sender:**UIGestureRecognizer**){
  print("test")
    //self.dropDown.isHidden = false
}

and :

let tap:UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(showpickerView(_:)))

You can test again.

Dhaval Bhadania
  • 3,090
  • 1
  • 20
  • 35
Ronald Vo
  • 64
  • 3
  • Thanks Ronald. ive updated func and tapgesture to func showpickerView(_ sender:UIGestureRecognizer) and let tap:UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(showpickerView(_:))) . Still doenst work – Phani.m Mar 24 '17 at 15:31
  • It show or not anything happen? did it print "test"? – Ronald Vo Mar 25 '17 at 05:33
  • No it doesn't print "test". Am unable to figure out whats wrong here. – Phani.m Mar 25 '17 at 20:40
0

This code works to unhide the UIPickerView

class FirstViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {

@IBOutlet var baseCurrency: UITextField!
@IBOutlet var excahngeCurrency: UITextField!
@IBOutlet var dropDown: UIPickerView!
@IBOutlet var currencyLabel: UILabel!

var list: [String: AnyObject] = [String: AnyObject]()
var dict: [String: AnyObject] = [String: AnyObject]()
var titles: [String] = []
var value: Int = 0

let food = ["app", "ban", "org"]
override func viewDidLoad() {
    super.viewDidLoad()

    currencyLabel.isUserInteractionEnabled = true
    let tap:UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(showpickerView(_:)))
    tap.numberOfTapsRequired = 1

    currencyLabel.addGestureRecognizer(tap)


let url:URL = URL(string: "https://openexchangerates.org/api/latest.json?app_id=7486951aeb994599a08cd7979bef3c03")!
    Alamofire.request(url).responseJSON(completionHandler: {
        response in

        switch response.result {

        case.success:

            self.list = response.result.value as! [String: AnyObject]

            self.dict = self.list["rates"] as! [String: AnyObject]

             self.value = self.dict["INR"] as! Int

            for (key, _) in self.dict
            {
                self.titles += [key]
            }


            print(self.value)
            print("test")

            print(self.titles)

            if (self.titles.count > 0) {

                self.dropDown.reloadAllComponents()


                print("teest")

            } else {
                print("there is no data for your picker view")
            }



        case .failure:
            print("error")


        }


    })

}

@objc func showpickerView(_ sender:UIGestureRecognizer){

  print("test")

   self.dropDown.isHidden = false

}

func numberOfComponents(in pickerView: UIPickerView) -> Int
{
    return 1
}

func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String?{


    return self.titles[row]


}

func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int
{
    return self.titles.count
}



func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int){


    currencyLabel.text = self.titles[row]
    self.dropDown.isHidden = true


}
Phani.m
  • 29
  • 4