Objective c uibarbuttonim目标/行动不起作用
我正在实现一个带有日期选择器和工具栏的视图。 我有以下代码:Objective c uibarbuttonim目标/行动不起作用,objective-c,action,uibarbuttonitem,target,Objective C,Action,Uibarbuttonitem,Target,我正在实现一个带有日期选择器和工具栏的视图。 我有以下代码: -(id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { self.toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(frame), 44)]; UIBarButtonItem *c
-(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(@"");
}
但当我单击按钮时,不会执行任何操作。不会调用这些方法。
你能看出我做错了什么吗?
谢谢
编辑:
结果发现有一个手势识别器捕捉到了触摸事件。
解决了这个问题。我怀疑你的日期选择器(与工具栏一样宽)在某种程度上阻止了你的按钮与用户接触 尝试将这两行代码反转为:
[self addSubview:self.datePicker];
[self addSubview:self.toolbar];
如果您尝试禁用UIgestureRecognitor对象并进行测试,您是否有UIgestureRecognitor对象。您是否可以编辑您的问题以显示“
didSelectDoneButton
”和“didSelectCancelButton
”方法(或至少是声明)的内容看起来像?这两个动作方法是这个UIView
类的一部分吗?出于好奇,我尝试将示例代码粘贴到我自己的UIView子类中,添加了两个选择器并对其进行了测试。。。它工作得很好。如果我们想提供帮助,你需要显示更多的代码。我是在一个视图上这样做的,这个视图是以模式呈现的,这是原因吗?