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