Question

What I'm trying to do here is basically do something when the UIPickerView is tapped. This is what I did.

I added a UIPickerView, added a tap gesture and tried it by logging a message to the screen, everything worked as expected I get the message every time I tap on the pickerView (pickerView is empty at this point). The funny thing is that this stops working as soon as I add items to the picker, to be exact, it actually stops working when I make the connection between the delegate/source and the ViewController (select the picker - connections inspector - control click the circle next to delegate and source and drag to viewController), I can see the list of items but if I tap on the pickerView I no longer see the log-message.

Here is the code I'm using to load the content to the pickerView, of course this code doesn't show neither the gesture nor the connection between delegate/source and the ViewController since these were done using XCode's interface builder.

// .h file

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController <UIPickerViewDataSource, UIPickerViewDelegate>

@end

// .m file

#import "ViewController.h"

@interface ViewController ()
{

}
@property NSArray *myArray;

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
  self.myArray = @[@"Apple", @"Orange", @"Banana"];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return [self.myArray count];
}
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    return self.myArray[row];
}

@end

Any idea why gesture stops working as soon as I connect delegate and source to ViewContoller?

Thanks a lot

Was it helpful?

Solution

It stopped working because this method is called:

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component

I think you should catch this method and do whatever you want here.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top