不能使用uitapgesturerecognizer解雇uidatepicker

时间:2013-04-06 12:51:08

标签: iphone ios6 uidatepicker

我想使用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

1 个答案:

答案 0 :(得分:0)

撰写self.tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissDatePicker:)];代替self.tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self.view action:@selector(dismissDatePicker:)];您为此动作添加了目标self.view,但您应将其更改为self

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

我认为这会对你有所帮助。