ios - 按钮操作和点击手势在 UIView XIB 上不起作用

标签 ios swift uiview uibutton xib

我使用 XIB 添加了自定义 UIView。我只是尝试执行按钮操作和点击手势,尽管为包括 ContentView 在内的所有元素启用了用户交互,但没有任何效果。

初始化UIView

-(void)initializeSubviews {
    self.backgroundColor = [UIColor clearColor];

    [[[NSBundle mainBundle]loadNibNamed:@"DatesView" owner:self options:nil]firstObject];
    [self addSubview:self.contentView];

   // self.contentView.frame = self.bounds;

    [self.fromDateButton addTarget:self action:@selector(tapOnfromDate:) forControlEvents:UIControlEventTouchUpInside];
    self.fromDateButton.backgroundColor = [UIColor redColor];


}

点击手势

UITapGestureRecognizer *fromDateTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapOnfromDate)];
    [datesView.contentView addGestureRecognizer:fromDateTapRecognizer];
    [datesView.fromMonthYearLabel addGestureRecognizer:fromDateTapRecognizer];
    [datesView.fromDayLabel addGestureRecognizer:fromDateTapRecognizer];

按钮操作

[datesView.fromDateButton addTarget:self action:@selector(tapOnfromDate) forControlEvents:UIControlEventTouchUpInside];

UIView界面

enter image description here

最佳答案

几件事...

1)通过在 View 子类中注释掉这一行:

// self.contentView.frame = self.bounds;

View 以 0,0 帧结束。按钮和标签等可见,因为 View 默认为 .clipsToBounds = NO,但对象不接收用户交互。

2) UITapGestureRecognizer 是单个实例。如果您将其添加到一个 View ,然后尝试将其添加到其他 View ,它只会存在于添加到的最后一个 View 中。

像这样尝试(一定要取消注释上面的行):

- (void)viewDidLoad {
    [super viewDidLoad];

    // do all the loading stuff here...

    // local declaration
    UITapGestureRecognizer *tapRecognizer;

    // Optional --- make *sure* user interaction is enabled
    [datesView.contentView setUserInteractionEnabled:YES];
    [datesView.fromMonthYearLabel setUserInteractionEnabled:YES];
    [datesView.fromDayLabel setUserInteractionEnabled:YES];

    // new recognizer
    tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapOnfromDate:)];
    // add it to contentView
    [datesView.contentView addGestureRecognizer:tapRecognizer];

    // new recognizer
    tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapOnfromDate:)];
    // add it to fromMonthYearLabel
    [datesView.fromMonthYearLabel addGestureRecognizer:tapRecognizer];

    // new recognizer
    tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapOnfromDate:)];
    // add it to fromDayLabel
    [datesView.fromDayLabel addGestureRecognizer:tapRecognizer];

    // add target action to fromDateButton
    [datesView.fromDateButton addTarget:self action:@selector(fromDateButtonTapped:) forControlEvents:UIControlEventTouchUpInside];

}

- (void) tapOnfromDate: (UITapGestureRecognizer *)recognizer {
    NSLog(@"Tapped! %@", recognizer.view);
}

- (void) fromDateButtonTapped: (id)sender {
    NSLog(@"Button Tapped! %@", sender);
}

关于ios - 按钮操作和点击手势在 UIView XIB 上不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59999448/

相关文章:

ios - 检查 child 会导致可扩展性差吗?

ios - 核心情节;使用 doubleForPlot 而不是 numberForPlot 时如何跳过数据

ios - 错误应用程序尝试以模态方式呈现事件 Controller Swift

ios - UIView 对象中的 UIGestureRecognizer

uiview - 如何使用动画缩放 uiview 及其内部的所有内容(其他自定义绘制的 uiview)?

ios - (iOS) 我如何对 UI 元素的翻译进行动画处理,例如两个 View Controller 之间转换中的标签和图像?

ios - Swift iOS 编程 - 如何处理错误(即防止应用程序崩溃/退出)

javascript - Urban Airship + Cordova 1.7+

ios - Sprite Kit 创建 iAdBanner

swift - 如何在 Swift 中使网络请求更快