2011-06-28 35 views
4

我只是试图解决UIPickerView - 导航栏上的按钮或选择器视图上方工具栏上的“完成”按钮。我已经实现了这两个按钮,并且我正在尝试解除选取器视图并退出第一响应者。UIToolBar上的Done按钮解除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; 

有人能帮助我吗?

+1

没有尝试过这个自己:到底什么是你的问题/问题? pickerView不会消失吗?如果这不起作用,你可以手动向下动画并调用removeFromSuperview,我猜。那有意义吗? –

+0

如果可能的话,我会尝试并复制它。如果我可以得到一个工作代码,我会分享我与你有什么 – justin

+0

@slev:谢谢你。 – Legolas

回答

22

虽然我确定我的测试应用程序比较简单得多,但希望结构仍适用于您的应用程序。

实质上,这就是我所做的一切。我在IB中设置了UIPickerView,UIDatePickerViewUITextField。 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]; 
} 

当文本框被激活,我称这种

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

于是最后,还有的操作方法

- (IBAction)pickerDoneClicked:(id)sender { 
    [datePicker removeFromSuperview]; 
    [pickerView removeFromSuperview]; 
    [textField resignFirstResponder]; 
} 

这一切对我的作品。所有东西都应该显示和删除。所以,如果幸运的话,这会做的伎俩对你太

+0

谢谢很多人! – Legolas

+0

完全没问题。我很高兴它帮助你出 – justin

+0

谢谢太slev ..这也适用于我。 +1 –

2
-(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窗口之外。

+0

我已经尝试过。我得到错误无法识别的选择器发送到实例'1290837' – Legolas

+0

不确定你做错了什么,因为我没有像slev那样精确输入你的代码,但是我希望你能够弄明白! :) –

2

在斯威夫特

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 
}