【问题标题】:Button actions and tap gestures are not working on UIView XIB按钮操作和点击手势在 UIView XIB 上不起作用
【发布时间】:2020-05-16 20:39:53
【问题描述】:

我用 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 界面

【问题讨论】:

  • 你能把你的xib文件传给我,我可以试试,因为我创建的工作正常,没有任何问题。在这里你可以通过 mshauket.developer@gmail.com
  • datesView 是你的 mainUIView 吗?

标签: ios swift uiview uibutton xib


【解决方案1】:

几件事...

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

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

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

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

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

- (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);
}

【讨论】:

    猜你喜欢
    • 2016-06-30
    • 1970-01-01
    • 2015-07-31
    • 1970-01-01
    • 1970-01-01
    • 2013-07-28
    • 1970-01-01
    • 2016-04-05
    相关资源
    最近更新 更多