简体   繁体   English

使用 UIToolBar 上的完成按钮关闭 UIPickerView

[英]Dismissing UIPickerView with Done button on UIToolBar

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.我只是在尝试关闭UIPickerView哪个更好——导航栏上的按钮或选择器视图上方工具栏上的“完成”按钮。 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?如何使用工具栏上的“完成”按钮关闭UIPickerView

This is my code for the UIToolBar :这是我的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.我在 IB 中设置了UIPickerViewUIDatePickerViewUITextField The pickerView's dataSource and delegate are both linked to File's Owner, as is the delegate of the textField. pickerView 的dataSourcedelegate都链接到 File 的 Owner,textField 的delegate也是如此。

In my header, I have them all declared with the following structure在我的 header 中,我将它们全部声明为以下结构

UISomething *object;
@property (nonatomic, retain) IBOutlet UISomething *object;

I've also got the protocols linked ( <UIPickerViewDelegate, UIPickerViewDataSource, UITextFieldDelegate> ).我还链接了协议( <UIPickerViewDelegate, UIPickerViewDataSource, UITextFieldDelegate> )。 In the implementation file, everything is synthesized.在实现文件中,一切都被综合了。 Then in viewDidLoad , I have this.然后在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];
}

When the textField becomes active, I call this当 textField 变为活动状态时,我称之为

- (void)textFieldDidBeginEditing:(UITextField *)textField {
    [self.view addSubview:pickerView];
    [self.view addSubview:datePicker];
}

Then finally, there's the action method最后是action方法

- (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因此,运气好的话,这也可以为您解决问题

In Swift在 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 UITextFieldDelegate

func textFieldShouldBeginEditing(textField: UITextField) -> Bool {
    textField.inputAccessoryView = inputToolbar

    return true
}
-(void)pickerDoneClicked:(id)sender {
    [pickerView removeFromSuperview];
}

Or if you want to dismiss it with an animation, change the view frame with UIView animations and then remove it from superview.或者,如果您想使用 animation 将其关闭,请使用 UIView 动画更改视图框架,然后将其从超级视图中删除。

[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
[UIView setAnimationDelay:1.0];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];

pickerView.frame = outOfScreenFrame;

[UIView commitAnimations];

where outOfScreenFrame is somewhere outside your UIApplication window.其中 outOfScreenFrame 位于您的 UIApplication window 之外的某个位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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