/ / Działanie przycisku nie działa na przycisku w widoku - ios, uibutton, uilabel

Działanie przycisku nie działa na przycisku w widoku - ios, uibutton, uilabel

W widoku używam etykiety jako hosta z przycisku. Etykieta zostanie dodana do self.view. Oba obiekty są wyświetlane zgodnie z oczekiwaniami, ale akcja zdefiniowana w przycisku nie jest uruchamiana. Oto kod:

datelabel = [[UILabel alloc] init];
datelabel.frame = CGRectMake(10, 200, 300, 40);
datelabel.backgroundColor = [UIColor whiteColor];
datelabel.textColor = [UIColor blueColor];
datelabel.font = [UIFont fontWithName:@"Verdana" size: 18.0];

NSDateFormatter *df = [[NSDateFormatter alloc] init];
df.dateStyle = NSDateFormatterMediumStyle;
datelabel.text = [NSString stringWithFormat:@"%@",
[df stringFromDate:[NSDate date]]];

CGRect buttonFrame = CGRectMake( 170, 8, 130, 25 );
UIButton *button = [[UIButton alloc] initWithFrame: buttonFrame];
[button setTitle: @"My Button" forState: UIControlStateNormal];
[button addTarget:self action:@selector(salvarFecha:) forControlEvents:
UIControlEventTouchUpInside];
[button setTitleColor: [UIColor redColor] forState: UIControlStateNormal];

[datelabel addSubview:button];

[self.view addSubview:datelabel];

A tutaj kod akcji przycisku:

- (void)salvarFecha: (id) sender{
NSLog(@"BOTON PULSADO");
}

Każda pomoc jest mile widziana ...

Odpowiedzi:

1 dla odpowiedzi № 1

Nigdy nie słyszałem, żeby ktokolwiek dodawał UIButton jako podwidok do UILabel. Dlaczego nie dodać go do self.view, tak jak robisz to z datelabel?


0 dla odpowiedzi nr 2

Wydaje mi się, że trzeba ustawić userInteractionEnabled do YES

Od UIView.h:

if set to NO, user events (touch, keys) are ignored and removed from the event queue.

W UILabel.h to pole jest NO domyślnie

Mam nadzieję, że to ci pomoże