gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 05:00:52 25 4
gpt4 key购买 nike

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

如何使用工具栏上的“完成”按钮关闭 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;

有人可以帮我吗?

最佳答案

虽然我确定我的测试应用相比之下要简单得多,但我最终还是让它工作了,所以希望该结构仍然适用于您的应用。

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

在我的标题中,我用以下结构声明了它们

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

我还链接了协议(protocol) ( <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];
}

最后是 Action 方法

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

这一切都适合我。一切都按应有的方式显示和删除。因此,运气好的话,这对你也有帮助

关于objective-c - 使用 UIToolBar 上的完成按钮关闭 UIPickerView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6509078/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com