gpt4 book ai didi

ios - UIpickerView 在同一 View Controller 中填充多个 uitextFields

转载 作者:行者123 更新时间:2023-11-29 12:57:16 25 4
gpt4 key购买 nike

您好,我正在开发一个 iphone 应用程序,我在其中填充了一个 uipickerview,它与一个 uitextfield 配合得很好。但是我有 4 个 uitextfield 需要填充,并且我对每个 UItextfield 使用相同的 uipickerview。我不知道如何实现这个东西。

Pickerviewcontroller.m

 arrChooseServices=[[NSMutableArray alloc]initWithObjects:@"A+ve",@"A-ve",@"B+ve",@"B-ve",@"AB+ve",@"AB-ve",@"O+ve",@"O-ve", nil];

// Create UITextfield

bloodGroup=[[UITextField alloc]initWithFrame:CGRectMake(150, 200, 120, 20)];
bloodGroup.borderStyle=UITextBorderStyleRoundedRect;
bloodGroup.backgroundColor=[UIColor clearColor];
bloodGroup.backgroundColor=[UIColor colorWithRed:0.662745 green:0.662745 blue:0.662745 alpha:0.5];

[bloodGroup setUserInteractionEnabled:NO];
bloodGroup.font=[UIFont systemFontOfSize:14.0];
bloodGroup.contentVerticalAlignment=UIControlContentVerticalAlignmentCenter;
bloodGroup.textAlignment=NSTextAlignmentCenter;
bloodGroup.delegate=self;

bloodGroup.inputView=self.chooseServicePicker;
bloodGroup.inputAccessoryView=self.accessoryView;
[self.view addSubview:bloodGroup];


-(UIPickerView *)chooseServicePicker
{
if(chooseServicePicker==nil)
{
chooseServicePicker=[[UIPickerView alloc]init];
chooseServicePicker.delegate=self;
chooseServicePicker.dataSource=self;
chooseServicePicker.showsSelectionIndicator=YES;
}
return chooseServicePicker;
}

-(UIToolbar *)accessoryView
{

if ( accessoryView == nil )
{
accessoryView = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 30)];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(onVehicleSelection)];

UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancel)];
accessoryView.items=[NSArray arrayWithObjects:doneButton,cancelButton, nil];

}
return accessoryView;

- (void)onVehicleSelection 
{

NSInteger row = [self.chooseServicePicker selectedRowInComponent:0];
bloodGroup.text=[arrChooseServices objectAtIndex:row];
[bloodGroup resignFirstResponder];
}

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

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

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component

{

return ( arrChooseServices == nil ) ? 0 : [arrChooseServices count];

}

-(void)cancel
{

bloodGroup.text=@"";

[bloodGroup resignFirstResponder];

}

这是我的代码。现在我想使用相同的 UIPickerview 在另一个文本字段中显示另一个数组值

gender=[NSMutableArray alloc]initWithObjectis:@"MALE",@"Female",nil];

我想在 Uipickerview 中添加以上数组值。如何编写代码。请给我任何想法。我是编程新手。提前致谢

最佳答案

您可以使用它来检查哪些文本将要 becomeFirstResponder

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField

并且基于这个条件

[textField isFirstResponder]

您可以检查需要显示的内容。

- (NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component 
{
if(textField1 isFirstResponder]
return [arrChooseServices objectAtIndex:row];

else if(textField2 isFirstResponder] return [otherArray count];
// write for every textfield other
}



- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
if(textField1 isFirstResponder] return 1;

else if(textField2 isFirstResponder] return 3;
//In this way you can do what you need to show in picker view.
return 1;
}



//Same condition you can use in every delegate and datasource method.

关于ios - UIpickerView 在同一 View Controller 中填充多个 uitextFields,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20755306/

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