gpt4 book ai didi

iOS 用 UIPickerView 替换键盘然后返回键盘

转载 作者:行者123 更新时间:2023-11-29 00:37:35 25 4
gpt4 key购买 nike

我的应用程序中有一个文本字段,用户可以在其中选择他们所属的组以及其他几个字段。当他们到达 Group TextField 时,它会弹出一个 UIPickerView,其中有多个已创建的选项,以及“创建新组”的选项。选择该选项后,我希望 UIPickerView 消失,并让键盘弹出,以便他们可以再次键入。我可以让选择器 View 出现并消失,但无法恢复键盘。这是到目前为止的代码。

-(void)addPickerView{

__block NSMutableArray *arr = [[NSMutableArray alloc] init];

PFQuery *rejectedNumber = [PFQuery queryWithClassName:@"Group"];
[rejectedNumber orderByAscending:@"GroupName"];
[rejectedNumber findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {



if (!objects) {
// Did not find any UserStats for the current user

NSLog(@"NotFound");
} else {
pickerArray = objects;
[myPickerView reloadAllComponents];

}
}];

//pickerArray = GoGo;
self.theView.signUpView.additionalField.delegate = self;
[self.theView.signUpView.additionalField setPlaceholder:@"Choose Group"];
myPickerView = [[UIPickerView alloc]init];
myPickerView.dataSource = self;
myPickerView.delegate = self;
myPickerView.showsSelectionIndicator = YES;
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc]
initWithTitle:@"Done" style:UIBarButtonItemStyleDone
target:self action:@selector(finishIt)];
UIToolbar *toolBar = [[UIToolbar alloc]initWithFrame:
CGRectMake(0, self.view.frame.size.height-
self.theView.signUpView.additionalField.frame.size.height-50, 320, 50)];
[toolBar setBarStyle:UIBarStyleBlackOpaque];
NSArray *toolbarItems = [NSArray arrayWithObjects:
doneButton, nil];
[toolBar setItems:toolbarItems];
self.theView.signUpView.additionalField.inputView = myPickerView;
self.theView.signUpView.additionalField.inputAccessoryView = toolBar;

}
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
return 1;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView
numberOfRowsInComponent:(NSInteger)component{
return [pickerArray count] + 1;
}

#pragma mark- Picker View Delegate

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:
(NSInteger)row inComponent:(NSInteger)component{
if (row == 0)
[self.theView.signUpView.additionalField.inputView removeFromSuperview];
[self.theView.signUpView.additionalField becomeFirstResponder];

}
else {

[self.theView.signUpView.additionalField setText:self.theGroup];
}
}
- (NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
NSString *groupName = @"Create New Group";
if (row != 0)
{
PFObject *object = pickerArray[row - 1]; // -1 to handle the array index
self.theGroup = object[@"GroupName"];
groupName = object[@"GroupName"];
}
return groupName;
}

最佳答案

我注意到的第一件事是,在 if 条件之后你错过了一个大括号:

if (row == 0)
[self.theView.signUpView.additionalField.inputView removeFromSuperview];
[self.theView.signUpView.additionalField becomeFirstResponder];

}

改用这个:

if (row == 0) {
[self.theView.signUpView.additionalField resignFirstResponder];
self.theView.signUpView.additionalField.inputView = nil;
[self.theView.signUpView.additionalField becomeFirstResponder];
}

关于iOS 用 UIPickerView 替换键盘然后返回键盘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40266152/

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