ios - 目标c如何根据选择第一个文本字段项目删除第二个和第三个文本字段中的项目
问题描述
我有一个 UIPickerview。我有一个json响应。我有一个三个文本字段。如果用户选择第一个文本字段显示选择器视图,则用户选择特定项目。第二个和第三个文本字段也一样。我的要求是如果用户在第一个选择器视图中选择特定项目,第二个和第三个选择器视图不显示该项目。第二和第三也一样。
-(void)viewDidLoad
{
dataArray = [[NSMutableArray alloc]initWithObjects:@"A+",@"A-",@"B+",@"B-",@"O+",@"O-", nil];
bloodGroup = [[UITextField alloc]initWithFrame:CGRectMake(10, logoImg.frame.origin.y+logoImg.frame.size.height+45, screenWidth-20, 50)];
bloodGroup.borderStyle = UITextBorderStyleRoundedRect;
bloodGroup.font = [UIFont systemFontOfSize:15];
bloodGroup.placeholder = @"Please Select Your Option";
bloodGroup.delegate = self;
[self.view addSubview:bloodGroup];
txtField1 = [[UITextField alloc]initWithFrame:CGRectMake(10, ansField1.frame.origin.y+ansField1.frame.size.height+45, screenWidth-20, 50)];
txtField1.borderStyle = UITextBorderStyleRoundedRect;
txtField1.font = [UIFont systemFontOfSize:15];
txtField1.placeholder = @"Please Select Your Option";
txtField1.delegate = self;
[self.view addSubview:txtField1];
txtField2 = [[UITextField alloc]initWithFrame:CGRectMake(10, ansField2.frame.origin.y+ansField2.frame.size.height+45, screenWidth-20, 50)];
txtField2.borderStyle = UITextBorderStyleRoundedRect;
txtField2.font = [UIFont systemFontOfSize:15];
txtField2.placeholder = @"Please Select Your Option";
txtField2.delegate = self;
[self.view addSubview:txtField2];
myPickerView = [[UIPickerView alloc] init];
[myPickerView setDataSource: self];
[myPickerView setDelegate: self];
myPickerView.showsSelectionIndicator = YES;
bloodGroup.inputView = myPickerView;
bloodGroup.inputAccessoryView = toolBar;
// txtField1
txtField1.inputView = myPickerView;
txtField1.inputAccessoryView = toolBar;
txtField2.inputView = myPickerView;
txtField2.inputAccessoryView = toolBar;
}
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
if (isBloodGroupFieldSelected) {
return 1;
}
else if(!isBloodGroupFieldSelected){
return 1;
}
else if(!isGenderGroupFieldSelected)
{
return 1;
}
return 0;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
if (isBloodGroupFieldSelected) {
return [dataArray count];
}
else if(!isBloodGroupFieldSelected)
{
return [dataArray count];
}
else if (!isGenderGroupFieldSelected)
{
return [dataArray count];
}
return 0;
}
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
if (isBloodGroupFieldSelected)
{
return dataArray[row];
}
else if((!isBloodGroupFieldSelected) && (isGenderGroupFieldSelected))
{
return dataArray[row];
}
else if(!isGenderGroupFieldSelected)
{
return dataArray[row];
}
return 0;
}
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
if (isBloodGroupFieldSelected) {
bloodGroup.text = dataArray[row];
}
else if((!isBloodGroupFieldSelected) && (isGenderGroupFieldSelected))
{
txtField1.text = dataArray[row];
}
else if(!isGenderGroupFieldSelected)
{
txtField2.text= dataArray[row];
}
}
- (void)textFieldDidBeginEditing:(UITextField *)textField {
if (textField == bloodGroup) {
isBloodGroupFieldSelected = YES;
}
else if (textField == txtField1){
isBloodGroupFieldSelected = NO;
isGenderGroupFieldSelected = YES;
}
else if (textField == txtField2) {
isGenderGroupFieldSelected = NO;
isBloodGroupFieldSelected = NO;
}
[myPickerView reloadAllComponents];
}
解决方案
-(无效)viewDidLoad {
dataArray = [[NSMutableArray alloc]initWithObjects:@"A+",@"A-",@"B+",@"B-",@"O+",@"O-", nil];
bloodGroup = [[UITextField alloc]initWithFrame:CGRectMake(10, logoImg.frame.origin.y+logoImg.frame.size.height+45, screenWidth-20, 50)];
bloodGroup.borderStyle = UITextBorderStyleRoundedRect;
bloodGroup.font = [UIFont systemFontOfSize:15];
bloodGroup.placeholder = @"请选择您的选项";
bloodGroup.delegate = 自我;
[self.view addSubview:bloodGroup];
txtField1 = [[UITextField alloc]initWithFrame:CGRectMake(10, ansField1.frame.origin.y+ansField1.frame.size.height+45, screenWidth-20, 50)];
txtField1.borderStyle = UITextBorderStyleRoundedRect;
txtField1.font = [UIFont systemFontOfSize:15];
txtField1.placeholder = @"请选择您的选项";
txtField1.delegate = self;
[self.view addSubview:txtField1];
txtField2 = [[UITextField alloc]initWithFrame:CGRectMake(10, ansField2.frame.origin.y+ansField2.frame.size.height+45, screenWidth-20, 50)];
txtField2.borderStyle = UITextBorderStyleRoundedRect;
txtField2.font = [UIFont systemFontOfSize:15];
txtField2.placeholder = @"请选择您的选项";
txtField2.delegate = self;
[self.view addSubview:txtField2];
myPickerView = [[UIPickerView alloc] init];
[myPickerView setDataSource: self];
[myPickerView setDelegate: self];
myPickerView.showsSelectionIndicator = 是;
bloodGroup.inputView = myPickerView;
bloodGroup.inputAccessoryView = 工具栏;
// txtField1
txtField1.inputView = myPickerView;
txtField1.inputAccessoryView = 工具栏;
txtField2.inputView = myPickerView;
txtField2.inputAccessoryView = 工具栏;
}
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
if (isBloodGroupFieldSelected) {
返回 1;
}
否则如果(!isBloodGroupFieldSelected){
返回 1;
}
否则如果(!isGenderGroupFieldSelected)
{
返回 1;
}
返回0;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
if (isBloodGroupFieldSelected) {
返回[数据数组计数];
}
否则如果(!isBloodGroupFieldSelected)
{
返回[数据数组计数];
}
否则如果(!isGenderGroupFieldSelected)
{
返回[数据数组计数];
}
返回0;
}
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
if (isBloodGroupFieldSelected)
{
返回数据数组[行];
}
否则 if((!isBloodGroupFieldSelected) && (isGenderGroupFieldSelected))
{
返回数据数组[行];
}
否则如果(!isGenderGroupFieldSelected)
{
返回数据数组[行];
}
返回0;
}
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
if (isBloodGroupFieldSelected) {
bloodGroup.text = dataArray[行];
}
否则 if((!isBloodGroupFieldSelected) && (isGenderGroupFieldSelected))
{
txtField1.text = dataArray[row];
}
否则如果(!isGenderGroupFieldSelected)
{
txtField2.text=dataArray[row];
}
}
- (void)textFieldDidBeginEditing:(UITextField *)textField {
if (textField == bloodGroup) {
isBloodGroupFieldSelected = YES;
}
否则 if (textField == txtField1){
isBloodGroupFieldSelected = 否;
isGenderGroupFieldSelected = YES;
}
否则 if (textField == txtField2) {
isGenderGroupFieldSelected = 否;
isBloodGroupFieldSelected = 否;
}
[myPickerView 重新加载所有组件];
}
- (void)textFieldDidBeginEditing:(UITextField *)textField {
if (textField == bloodGroup) {
isBloodGroupFieldSelected = YES;
}else if (textField == txtField1){
isBloodGroupFieldSelected = 否;
isGenderGroupFieldSelected = YES;
}
否则 if (textField == txtField2){
isGenderGroupFieldSelected = 否;
isBloodGroupFieldSelected = 否;
}
[myPickerView 重新加载所有组件];
}
- (void)viewWillAppear:(BOOL)动画{
[超级viewWillAppear:动画];
[myPickerView selectRow:0 inComponent:0 动画:YES];
}
-(无效)完成:(id)发件人
{
[myPickerView selectRow:0 inComponent:0 动画:YES]; // 2020 年 4 月 27 日
[血组辞职FirstResponder];
[txtField1 resignFirstResponder];
[txtField2 resignFirstResponder];
}
推荐阅读
- python - 如何返回导致包含 True 标志的关键字
- android - 保存到画廊的图像被旋转并且日期错误(1970)
- php - laravel 如何从自定义请求对象进行验证?
- javascript - 从 URL(动态数据)在页面中发布 JSON 数据
- javascript - 带有动作的倒数计时器
- c# - 如何将特定的 json 字符串反序列化为 ASP.NET MVC 模型?
- reactjs - 你不应该在外面使用
错误 - python - Pandas:如果 df1 中的子字符串存在于 df2 的字符串中,则加入两个数据帧(如果字符串包含子字符串)
- sql - 合并2个没有主键的表
- html - 如何在用户界面(AngularJs/HTML)中显示 git 命令结果?