0

我想使用 uidatepicker 输入日期。我已将 UITextField 的输入视图更改为 UIDatePicker 对象。DatePicker 正在显示,没问题。但是后来我添加了一个 UITapGestureRecognizer 以便当用户在日期选择器之外点击时,日期选择器将隐藏。在这里,我遇到了一个例外。我无法隐藏日期选择器

接口文件:

@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 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

例外:发送到实例的无法识别的选择器

平台详情:

XCode 4.5.2

部署目标:6.0

基础 SDK:6.0

4

1 回答 1

0

self.tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissDatePicker:)];而不是您为此手势self.tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self.view action:@selector(dismissDatePicker:)];添加了目标,但您应该将其更改为.self.viewself

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

我想这会对你有所帮助。

于 2013-04-06T13:49:05.803 回答