Dismissing UIPickerView with Done button on UIToolBar

大城市里の小女人 提交于 2020-01-09 10:29:12

问题


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?


回答1:


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




回答2:


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




回答3:


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

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

    return true
}


来源:https://stackoverflow.com/questions/6509078/dismissing-uipickerview-with-done-button-on-uitoolbar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!