gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 10:26:43 26 4
gpt4 key购买 nike

我使用 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com