使用UIToolBar上的完成按钮来取消UIPickerView [英] Dismissing UIPickerView with Done button on UIToolBar
问题描述
我只是试着解决一个 UIPickerView
- 导航栏上的按钮或选择器上方的工具栏上的完成按钮视图。我已经实现了两个按钮,我试图关闭选择器视图并辞职第一个响应者。
如何解除 UIPickerView $
这是我的代码 UIToolBar
:
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;
有人可以帮我吗?
我得到它的工作在我的尽头,虽然我相信我的测试应用程序是比较简单得多,所以希望结构仍然适用于你的。
实质上,这就是我做的。我有一个 UIPickerView
, UIDatePickerView
和 UITextField
在IB。 pickerView的 dataSource
和委托
都链接到文件的所有者,委托
of textField。
在我的标题中, pre> UISomething * object;
@property(nonatomic,retain)IBOutlet UISomething * object;
我还有协议链接UITextFieldDelegate> )。在实现文件中,一切都是合成的。然后在 viewDidLoad
中,我有这个。
- (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];
}
当textField变为活动状态时,我将其称为
- (void)textFieldDidBeginEditing:(UITextField *)textField {
[self.view addSubview:pickerView];
[self.view addSubview:datePicker];
}
最后,还有action方法
- (IBAction)pickerDoneClicked:(id)sender {
[datePicker removeFromSuperview];
[pickerView removeFromSuperview];
[textField resignFirstResponder];
}
这一切都适用于我。一切都显示和删除,因为它应该。所以有了运气,这也会为你的秘诀
I am just trying out which is better with regards to dismissing a UIPickerView
-- a button on the navigation bar or a "Done" button on a toolbar above the picker view. I have implemented both buttons, and I am trying to dismiss the picker view and resign first responder.
How can I dismiss the UIPickerView
with the "Done" Button on the toolbar?
This is my code for the UIToolBar
:
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;
Could someone help me with this?
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
这篇关于使用UIToolBar上的完成按钮来取消UIPickerView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!