I got it working on my end, though I'm sure my test app is much much simpler in comparison, so hopefully the structure still works for yours.
In essence, this is all I did. I have a UIPickerView
, UIDatePickerView
, and UITextField
set up in IB. The pickerView's dataSource
and delegate
are both linked to File's Owner, as is the delegate
of the textField.
In my header, I have them all declared with the following structure
UISomething *object;
@property (nonatomic, retain) IBOutlet UISomething *object;
I've also got the protocols linked (<UIPickerViewDelegate, UIPickerViewDataSource, UITextFieldDelegate>
). In the implementation file, everything is synthesized. Then in viewDidLoad
, I have this.
- (void)viewDidLoad
{
UIToolbar* keyboardDoneButtonView = [[UIToolbar alloc] init];
keyboardDoneButtonView.barStyle = UIBarStyleBlack;
keyboardDoneButtonView.translucent = YES;
keyboardDoneButtonView.tintColor = nil;
[keyboardDoneButtonView sizeToFit];
UIBarButtonItem* doneButton = [[[UIBarButtonItem alloc] initWithTitle:@"Done"
style:UIBarButtonItemStyleBordered target:self
action:@selector(pickerDoneClicked:)] autorelease];
[keyboardDoneButtonView setItems:[NSArray arrayWithObjects:doneButton, nil]];
textField.inputAccessoryView = keyboardDoneButtonView;
[datePicker removeFromSuperview];
[pickerView removeFromSuperview];
[super viewDidLoad];
}
When the textField becomes active, I call this
- (void)textFieldDidBeginEditing:(UITextField *)textField {
[self.view addSubview:pickerView];
[self.view addSubview:datePicker];
}
Then finally, there's the action method
- (IBAction)pickerDoneClicked:(id)sender {
[datePicker removeFromSuperview];
[pickerView removeFromSuperview];
[textField resignFirstResponder];
}
This all works for me. Everything gets displayed and removed as it should. So with any luck, this will do the trick for you too
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…