Действия кнопок и касания не работают в UIView XIB - PullRequest
0 голосов
/ 31 января 2020

Я добавил пользовательский UIView с XIB. Я только что попытался выполнить действия кнопок и прикосновений, ничего не работает, хотя взаимодействие с пользователем включено для всех элементов, включая ContentView.

Intializing 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 Ответ

0 голосов
/ 31 января 2020

Пара вещей ...

1) Комментируя эту строку в подклассе вашего представления:

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

представление заканчивается рамкой 0,0. Кнопка и метки et c видны, потому что для вида по умолчанию установлено значение .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);
}
...