gpt4 book ai didi

iphone - 为键盘切换 UIPicker

转载 作者:行者123 更新时间:2023-11-29 13:07:18 25 4
gpt4 key购买 nike

我已经对此进行了研究,但只发现了如何将键盘切换为键盘。现在我有一个 UIPicker,它会在点击文本字段时出现。在选择器中有三个选项,第三个是自定义。 What I want to happen is when custom is selected the UIPicker is replaced with a keyboard.我猜这将通过 if (select == 2) { } 方法完成。我不需要一大堆代码,只需要替换 Picker 的方法。我还想制作一个带有后退按钮的工具栏以返回选择器。我目前有一个带有 doneButton 的工具栏来释放选择器,但我需要在键盘出现时显示一个按钮,并且该按钮能够将键盘切换回我的选择器。

到目前为止,我的选择器、toolabr 和文本字段是:

...

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

NSArray *array = [[NSArray alloc] initWithObjects:@"...",@"...",@"Custom", nil];
self.PickerData = array;

...

UIToolbar *toolBar = [[UIToolbar alloc] init];
toolBar.barStyle = UIBarStyleBlackOpaque;
[toolBar sizeToFit];

...

UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
target:self
action:nil];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone
target:self
action:@selector(releasePicker)];


Picker = [[UIPickerView alloc] init];
Picker.showsSelectionIndicator = YES;
Picker.delegate = self;
doneButton.image = [UIImage imageNamed:@"button.png"];

[toolBar setItems:@[flexSpace, doneButton] animated:YES];
self.habitField.inputAccessoryView = toolBar;

[self.habitField setEnabled: YES];

[self.habitField setInputView:Picker];

}
- (void)releasePicker {
[self.habitField resignFirstResponder];
}

...

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 1;
}

-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
return [PickerData count];
}

-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
return [self.PickerData objectAtIndex:row];
}

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
int select = row;
if (select == 0) {

...
}
if (select == 1) {

...
}
if (select == 2) {

...

}
}


@end

最佳答案

if (select == 2) {
[self.habitField resignFirstResponder];
[self.habitField setInputView:nil];
[self.habitField becomeFirstResponder];
}

这基本上是隐藏 UIPickerView,然后将其移除,然后弹出键盘。

如果你想要 UIPicker 启动,请使用此代码:

[self.habitField resignFirstResponder];
[self.habitField setInputView:PickerData];
[self.habitField becomeFirstResponder];

关于iphone - 为键盘切换 UIPicker,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18283027/

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