在UIToolBar上使用Done按钮关闭UIPickerView

时间:2011-06-28 15:36:34

标签: objective-c ios cocoa-touch uipickerview 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;

有人可以帮我这个吗?

3 个答案:

答案 0 :(得分:22)

我得到了它的工作,虽然我确信我的测试应用程序相比要简单得多,所以希望这个结构对你来说仍然有效。

从本质上讲,这就是我所做的一切。我在IB中设置了UIPickerViewUIDatePickerViewUITextField。 pickerView的dataSourcedelegate都链接到文件的所有者,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];
}

这一切都适合我。一切都得到了显示和删除。所以运气好的话,这也会为你做到这一点

答案 1 :(得分:2)

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

或者如果你想用动画关闭它,用UIView动画改变视图框,然后从superview中删除它。

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

pickerView.frame = outOfScreenFrame;

[UIView commitAnimations];

其中outOfScreenFrame位于UIApplication窗口之外的某个位置。

答案 2 :(得分:2)

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