简体   繁体   中英

UIBarButtonItem don't work on tap

I get some problem to trigger my toolbar button on my datepicker. My datepicker is showing perfectly but when i click on done item button nothing append in log . I don't know why it's not working.

any help would be appreciated :)

-(void) dismiss:(id){
  NSLog("test");
}

-(IBAction) datePicker:(id)sender{
  ...
  ...
  UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height, 320, 44)];
  toolBar.tag = 1;
  toolBar.barStyle = UIBarStyleBlackTranslucent;

  UIBarButtonItem *spacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
  UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(dismiss:)];
  [toolBar setItems:[NSArray arrayWithObjects:spacer, doneButton, nil]];
}

I finally found an alternative , the solution is to use through a textfield like this code:

I use ARC

UIToolbar *toolBar = [UIToolbar alloc] init];     
UITextField * textfield = [[UITextField alloc]init];
UIDatePicker *datePicker = [[[UIDatePicker alloc] init]];

toolbar.barStyle = UIBarStyleDefault;

datePicker.datePickerMode = UIDatePickerModeDate;
[datePicker addTarget:self action:@selector(datePickerValueChanged:) forControlEvents:UIControlEventValueChanged];

textField.inputView = datePicker;
textfield.inputAccessoryView = toolBar;
[self.view addSubview:textfield];
[textfield becomeFirstResponder];

Hope this work for you too.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM