0

I want to take a date input using uidatepicker. I have changed the input view of my UITextField to a UIDatePicker object. The DatePicker is displaying, no problem. But then I added a UITapGestureRecognizer so that when the user taps outside the date picker, then date picker will hide. Here I am getting an exception. I cannot hide the date picker.

interface file:

@interface ViewController : UIViewController <UITextFieldDelegate>

{
    UIDatePicker *anewPicker;
    UITapGestureRecognizer *tapGestureRecognizer;
}

@property (weak, nonatomic) IBOutlet UILabel *labelOutlet;

@property (weak, nonatomic) IBOutlet UITextField *startTimeTxtOutlet;

@property (weak, nonatomic) IBOutlet UITextField *endTimeTxtOutlet;

@property UIDatePicker *anewPicker;

@property UITapGestureRecognizer *tapGestureRecognizer;

@end

implementation file:

@implementation ViewController

@synthesize anewPicker;
@synthesize tapGestureRecognizer;

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.anewPicker = [[UIDatePicker alloc] initWithFrame:[self.view bounds]];
    [anewPicker addTarget:self action:@selector(enteredStartDate:)     forControlEvents:UIControlEventValueChanged];
    self.startTimeTxtOutlet.inputView = self.anewPicker;
    self.endTimeTxtOutlet.inputView = self.anewPicker;


    self.tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self.view     action:@selector(dismissDatePicker:)];
    self.tapGestureRecognizer.numberOfTapsRequired = 1;
    [self.view addGestureRecognizer:self.tapGestureRecognizer];

}



-(void)enteredStartDate:(id)sender
{
    NSLog(@"start date %@",[sender date]);

}

-(void)enteredEndDate:(id)sender
{
    NSLog(@"end date %@",[sender date]);
}



-(void)dismissDatePicker:(UIGestureRecognizer*)gestureRecognizer
{
    //in this function i get the exception
    //other functions are ok

    NSLog(@"tap");

    self.anewPicker.hidden = YES;
}


@end

Exception: unrecognized selector sent to instance

Platform details:

XCode 4.5.2

deployment target: 6.0

base SDK: 6.0

Shuaib
  • 779
  • 2
  • 13
  • 47
  • `self.tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self.view action:@selector(dismissDatePicker:)]; ` to `self.tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissDatePicker:)];`? – Larme Apr 06 '13 at 13:01
  • same code i copy and past its working fine yaar – Nag_iphone Apr 06 '13 at 13:09
  • 1
    Have you maintained IBOutlet Connections properly dude ? – Manu Apr 06 '13 at 13:12
  • yes manohar is correct check whether IBOutlet Connections are good or not? – Nag_iphone Apr 06 '13 at 13:13

1 Answers1

0

Write self.tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissDatePicker:)]; instead of self.tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self.view action:@selector(dismissDatePicker:)]; You added the target self.view for this gesture but you should change it to self.

   self.tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissDatePicker:)];
    self.tapGestureRecognizer.delegate =self;
    self.tapGestureRecognizer.numberOfTapsRequired = 1;
    [self.view addGestureRecognizer:self.tapGestureRecognizer];

I think it will be helpful to you.

Prasad G
  • 6,702
  • 7
  • 42
  • 65