我只是在尝试在取消UIPickerView
(导航栏上的按钮)或选择器视图上方工具栏上的"完成"按钮方面,哪一个更好。我已经实现了这两个按钮,我正试图驳回picker视图并辞去第一响应者的职务。
如何使用工具栏上的"完成"按钮解除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;
有人能帮我吗?
我已经完成了它,尽管我相信我的测试应用程序相比之下要简单得多,所以希望这个结构仍然适用于您。
从本质上讲,这就是我所做的一切。我在IB中设置了UIPickerView
、UIDatePickerView
和UITextField
。pickerView的dataSource
和delegate
都链接到File的Owner,textField的delegate
也是如此。
在我的标题中,我用以下结构声明了它们
UISomething *object;
@property (nonatomic, retain) IBOutlet UISomething *object;
我还链接了协议(<UIPickerViewDelegate, UIPickerViewDataSource, 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];
}
最后,还有行动方法
- (IBAction)pickerDoneClicked:(id)sender {
[datePicker removeFromSuperview];
[pickerView removeFromSuperview];
[textField resignFirstResponder];
}
这一切对我都有效。所有东西都会按原样显示和删除。因此,如果运气好的话,这也会对你有用。
-(void)pickerDoneClicked:(id)sender {
[pickerView removeFromSuperview];
}
或者,如果要使用动画来消除它,请使用UIView动画更改图幅,然后将其从超级视图中删除。
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
[UIView setAnimationDelay:1.0];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
pickerView.frame = outOfScreenFrame;
[UIView commitAnimations];
其中outOfScreenFrame位于UIApplication窗口之外。
在Swift 中
lazy var inputToolbar: UIToolbar = {
var toolbar = UIToolbar()
toolbar.barStyle = .Default
toolbar.translucent = true
toolbar.sizeToFit()
var doneButton = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.Bordered, target: self, action: "inputToolbarDonePressed")
var spaceButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: nil, action: nil)
toolbar.setItems([spaceButton, doneButton], animated: false)
toolbar.userInteractionEnabled = true
return toolbar
}()
func inputToolbarDonePressed() {
view.endEditing(true)
}
UITextFieldDelegate
func textFieldShouldBeginEditing(textField: UITextField) -> Bool {
textField.inputAccessoryView = inputToolbar
return true
}