我正在实现一个带有日期选择器和工具栏的视图。我有以下代码:
-(id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(frame), 44)];
UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel
target:self
action:@selector(didSelectCancelButton)];
UIBarButtonItem *flexible = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone
target:self
action:@selector(didSelectDoneButton)];
NSArray *buttons = @[cancelButton, flexible, doneButton];
[self.toolbar setItems:buttons
animated:NO];
self.datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 50, CGRectGetWidth(frame), 216)];
[self addSubview:self.datePicker];
[self addSubview:self.toolbar];
}
return self;
}
- (IBAction)didSelectCancelButton {
if ([self.delegate respondsToSelector:@selector(datePickerDidCancelDateSelection:)]) {
[self.delegate datePickerDidCancelDateSelection:self];
}
}
- (IBAction)didSelectDoneButton {
NSLog(@"");
}但当我单击按钮时,没有执行任何操作。不会调用这些方法。你能说出我做错了什么吗?谢谢
编辑:原来有一个手势识别器来捕捉触摸事件。修复解决了此问题。
发布于 2013-07-10 04:29:33
我怀疑你的日期选择器(它和工具栏一样宽)在某种程度上阻碍了你的按钮获得用户的触摸。
尝试将这两行代码反转为:
[self addSubview:self.datePicker];
[self addSubview:self.toolbar];发布于 2015-02-04 19:33:44
您是否有任何UIGestureRecognizer对象,如果您确实尝试禁用它们并进行测试。
https://stackoverflow.com/questions/17557473
复制相似问题