UIBarButtonItem目标/操作不起作用

时间:2013-07-09 20:23:19

标签: 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 *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(@"");
}

但是当我点击按钮时,不会执行任何操作。不调用这些方法。 你能说出我做错了什么吗? 感谢

编辑: 原来有一个手势识别器正在捕捉触摸事件。 修复解决了这个问题。

2 个答案:

答案 0 :(得分:0)

我怀疑你的日期选择器(它与工具栏一样宽)会以某种方式阻止你的按钮触及用户。

尝试将这两行反转为:

[self addSubview:self.datePicker];
[self addSubview:self.toolbar];

答案 1 :(得分:0)

您是否有任何UIGestureRecognizer对象,如果您尝试禁用它们并进行测试。

相关问题