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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6509078/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 20:33:04  来源:igfitidea点击:

Dismissing UIPickerView with Done button on UIToolBar

objective-cioscocoa-touchuipickerviewuitoolbar

提问by Legolas

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.

我只是在尝试在关闭UIPickerView导航栏上的按钮或选择器视图上方工具栏上的“完成”按钮方面哪个更好。我已经实现了两个按钮,我正在尝试关闭选择器视图并退出第一响应者。

How can I dismiss the UIPickerViewwith 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?

有人可以帮我解决这个问题吗?

回答by justin

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 UITextFieldset up in IB. The pickerView's dataSourceand delegateare both linked to File's Owner, as is the delegateof the textField.

本质上,这就是我所做的一切。我有一个UIPickerView, UIDatePickerView, 并UITextField在 IB 中设置。pickerView 的dataSourcedelegate都链接到文件的所有者,文本字段也是如此delegate

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.

我还获得了链接的协议 ( <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];
}

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

这一切都对我有用。一切都按原样显示和删除。所以运气好的话,这也能帮到你

回答by Filipe Pina

-(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.

或者,如果您想使用动画关闭它,请使用 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 窗口之外的某处。

回答by Sahil Kapoor

In 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
}