gpt4 book ai didi

ios - 如何在iOS中使用TextField显示基于先前PickerView的PickerView值?

转载 作者:行者123 更新时间:2023-12-01 16:03:19 25 4
gpt4 key购买 nike

我有三个TextFields regionText sectionText distributionText。如果我单击“第一个文本字段” regionText,则应该显示PickerView regionPicker和Array值regionArray。如果我没有选择sectionText中的值就单击distributionTextregionText,则应显示警报消息。

然后,基于regionText

ViewController.m

- (void)viewDidLoad
{
[super viewDidLoad];

//static Array for RegionPicker
NSArray *regionArray = [[NSArray alloc]initWithObjects:@"Chennai-south",@"Chennai-north",@"Villupuram", nil];

//static Array for SectionPicker
NSArray *chennaiSouthArray = [[NSArray alloc]initWithObjects:@"A",@"B",@"C", nil];
NSArray *chennaiNorthArray = [[NSArray alloc]initWithObjects:@"D",@"E",@"F", nil];
NSArray *villupuramArray = [[NSArray alloc]initWithObjects:@"D",@"E",@"F", nil];

//static Array for DistributionPicker
NSArray *aArray = [[NSArray alloc]initWithObjects:@"a",@"b", nil];
NSArray *bArray = [[NSArray alloc]initWithObjects:@"c",@"d", nil];
NSArray *cArray = [[NSArray alloc]initWithObjects:@"e",@"f", nil];

NSArray *dArray = [[NSArray alloc]initWithObjects:@"g",@"h", nil];
NSArray *eArray = [[NSArray alloc]initWithObjects:@"i",@"j", nil];


//viewForPicker
_viewForPicker = [[UIView alloc]initWithFrame:CGRectMake(0,self.view.frame.size.height, self.view.frame.size.width,260)];
_viewForPicker.backgroundColor = [UIColor blackColor];
[self.view addSubview:_viewForPicker];

//regionPickerView
_regionPicker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 44, self.view.frame.size.width, 216)];
_regionPicker.delegate = self;
_regionPicker.dataSource = self;
_regionPicker.showsSelectionIndicator = YES;
[_regionPicker setBackgroundColor:[UIColor lightGrayColor]];
[_viewForPicker addSubview:_regionPicker];


//ToolBar
UIToolbar *toolBar= [[UIToolbar alloc] initWithFrame:CGRectMake(0,0,self.view.frame.size.width,44)];
[toolBar setBarStyle:UIBarStyleBlackOpaque];
UIBarButtonItem *barButtonDone = [[UIBarButtonItem alloc] initWithTitle:@"Done"
style:UIBarButtonItemStyleBordered target:self action:@selector(hidePickerView:)];
toolBar.items = @[barButtonDone];
barButtonDone.tintColor=[UIColor lightTextColor];
[barButtonDone setEnabled:YES];
[_viewForPicker addSubview:toolBar];

//SettingLabels
[self setView];
}

-(void)hidePickerView:(id)sender
{

[UIView animateWithDuration:0.5 delay:0.1 options:UIViewAnimationOptionCurveEaseIn animations:^{
_viewForPicker.frame = CGRectMake(0, self.view.frame.size.height, 320, 260);
} completion:^(BOOL finished){
}];
}

-(void)setView
{

//regionLabel
UILabel *regionLabel = [[UILabel alloc]initWithFrame:CGRectMake(self.view.frame.origin.x+30, self.view.frame.origin.y+30, 60, 25)];
[regionLabel setText:@"Region"];
[self.view addSubview:regionLabel];

//regionText
UITextField *regionText = [[UITextField alloc]initWithFrame:CGRectMake(self.view.frame.origin.x+120, self.view.frame.origin.y+30, 150, 25)];
[regionText setBorderStyle:UITextBorderStyleRoundedRect];
[regionText setPlaceholder:@"selectRegion"];
[regionText setTag:0];
[regionText setDelegate:self];
[self.view addSubview:regionText];

//sectionLabel
UILabel *sectionLabel = [[UILabel alloc]initWithFrame:CGRectMake(self.view.frame.origin.x+30, self.view.frame.origin.y+120, 60, 25)];
[sectionLabel setText:@"Section"];
[self.view addSubview:sectionLabel];

//sectionText
UITextField *sectionText = [[UITextField alloc]initWithFrame:CGRectMake(self.view.frame.origin.x+120, self.view.frame.origin.y+120, 150, 25)];
[sectionText setBorderStyle:UITextBorderStyleRoundedRect];
[sectionText setPlaceholder:@"selectSection"];
[sectionText setTag:1];
[sectionText setDelegate:self];
[self.view addSubview:sectionText];


//distributionLabel
UILabel *distributionLabel = [[UILabel alloc]initWithFrame:CGRectMake(self.view.frame.origin.x+30, self.view.frame.origin.y+210, 90, 25)];
[distributionLabel setText:@"Distribution"];
[self.view addSubview:distributionLabel];

//distributionText
UITextField *distributionText = [[UITextField alloc]initWithFrame:CGRectMake(self.view.frame.origin.x+120, self.view.frame.origin.y+210, 150, 25)];
[distributionText setBorderStyle:UITextBorderStyleRoundedRect];
[distributionText setPlaceholder:@"selectDistribution"];
[distributionText setTag:2];
[distributionText setDelegate:self];
[self.view addSubview:distributionText];

}

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
// Show UIPickerView


if (textField isEqual:@"regionText")
{
NSLog(@"Entering Textfield Begin");
[UIView animateWithDuration:0.5 delay:0.1 options:UIViewAnimationOptionCurveEaseIn animations:^{
_viewForPicker.frame = CGRectMake(0, self.view.frame.size.height-260,self.view.frame.size.width, 260);
}
completion:^(BOOL finished){
}];
}

elseif(textField isEqual:@"sectionText")
{
//What i have to Write in This.Please Update.
}
else
{
//What i have to Write in This.Please Update.
}
return NO;
}

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)thePickerView {

return 1;
}

- (NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component
{
if([pickerView isEqual:@"regionPicker"])
return [regionArray count];

elseif([pickerView isEqual:@"regionPicker"])
{
//What i have to write.Pls update.
}
else
{
//What i have to write.Pls update.
}

}

- (NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
//What i have to write.Pls update.
}

1.如何使用单个UIView viewforPicker显示PickerView?

2.如何显示PickerView值取决于以前的PickerView值?

最佳答案

这是经过测试的更新代码:

首先在.h文件中声明:

UIView *_viewForPicker;
UIPickerView *_regionPicker;
NSArray *regionArray;
NSArray *chennaiSouthArray;
NSArray *chennaiNorthArray;
NSArray *villupuramArray;
int selectionVal;

之后在.m文件中:
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.leftBarButtonItem = self.editButtonItem;
//static Array for RegionPicker
regionArray = [[NSArray alloc] initWithObjects:@"Chennai-south",@"Chennai-north",@"Villupuram", nil];

//static Array for SectionPicker
chennaiSouthArray = [[NSArray alloc]initWithObjects:@"A",@"B",@"C", nil];
chennaiNorthArray = [[NSArray alloc]initWithObjects:@"D",@"E",@"F", nil];
villupuramArray = [[NSArray alloc]initWithObjects:@"D",@"E",@"F", nil];

//static Array for DistributionPicker
NSArray *aArray = [[NSArray alloc]initWithObjects:@"a",@"b", nil];
NSArray *bArray = [[NSArray alloc]initWithObjects:@"c",@"d", nil];
NSArray *cArray = [[NSArray alloc]initWithObjects:@"e",@"f", nil];
NSArray *dArray = [[NSArray alloc]initWithObjects:@"g",@"h", nil];
NSArray *eArray = [[NSArray alloc]initWithObjects:@"i",@"j", nil];


//viewForPicker
_viewForPicker = [[UIView alloc]initWithFrame:CGRectMake(0,self.view.frame.size.height, self.view.frame.size.width,260)];
_viewForPicker.backgroundColor = [UIColor blackColor];
[self.view addSubview:_viewForPicker];

//regionPickerView
_regionPicker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 44, self.view.frame.size.width, 216)];
_regionPicker.delegate = self;
_regionPicker.dataSource = self;
_regionPicker.showsSelectionIndicator = YES;
[_regionPicker setBackgroundColor:[UIColor lightGrayColor]];
[_viewForPicker addSubview:_regionPicker];

//ToolBar
UIToolbar *toolBar= [[UIToolbar alloc] initWithFrame:CGRectMake(0,0,self.view.frame.size.width,44)];
[toolBar setBarStyle:UIBarStyleBlackOpaque];
UIBarButtonItem *barButtonDone = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStylePlain target:self action:@selector(hidePickerView:)];
toolBar.items = @[barButtonDone];
barButtonDone.tintColor=[UIColor lightTextColor];
[barButtonDone setEnabled:YES];
[_viewForPicker addSubview:toolBar];

//SettingLabels
[self setView];
}

- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
}

-(void)hidePickerView:(id)sender {
[UIView animateWithDuration:0.5 delay:0.1 options:UIViewAnimationOptionCurveEaseIn animations:^{
_viewForPicker.frame = CGRectMake(0, self.view.frame.size.height, 320, 260);
} completion:^(BOOL finished){
}];
}

-(void)setView {
//regionLabel
UILabel *regionLabel = [[UILabel alloc]initWithFrame:CGRectMake(self.view.frame.origin.x+30, self.view.frame.origin.y+30, 60, 25)];
[regionLabel setText:@"Region"];
[self.view addSubview:regionLabel];

//regionText
UITextField *regionText = [[UITextField alloc]initWithFrame:CGRectMake(self.view.frame.origin.x+120, self.view.frame.origin.y+30, 150, 25)];
[regionText setBorderStyle:UITextBorderStyleRoundedRect];
[regionText setPlaceholder:@"selectRegion"];
[regionText setTag:51];
[regionText setDelegate:self];
[self.view addSubview:regionText];

//sectionLabel
UILabel *sectionLabel = [[UILabel alloc]initWithFrame:CGRectMake(self.view.frame.origin.x+30, self.view.frame.origin.y+120, 60, 25)];
[sectionLabel setText:@"Section"];
[self.view addSubview:sectionLabel];

//sectionText
UITextField *sectionText = [[UITextField alloc] initWithFrame:CGRectMake(self.view.frame.origin.x+120, self.view.frame.origin.y+120, 150, 25)];
[sectionText setBorderStyle:UITextBorderStyleRoundedRect];
[sectionText setPlaceholder:@"selectSection"];
[sectionText setTag:52];
[sectionText setDelegate:self];
[self.view addSubview:sectionText];

//distributionLabel
UILabel *distributionLabel = [[UILabel alloc]initWithFrame:CGRectMake(self.view.frame.origin.x+30, self.view.frame.origin.y+210, 90, 25)];
[distributionLabel setText:@"Distribution"];
[self.view addSubview:distributionLabel];

//distributionText
UITextField *distributionText = [[UITextField alloc]initWithFrame:CGRectMake(self.view.frame.origin.x+120, self.view.frame.origin.y+210, 150, 25)];
[distributionText setBorderStyle:UITextBorderStyleRoundedRect];
[distributionText setPlaceholder:@"selectDistribution"];
[distributionText setTag:53];
[distributionText setDelegate:self];
[self.view addSubview:distributionText];

}

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
// Show UIPickerView
if (textField.tag == 51){
NSLog(@"Entering Textfield Begin");
selectionVal = 1;
[UIView animateWithDuration:0.5 delay:0.1 options:UIViewAnimationOptionCurveEaseIn animations:^{
_viewForPicker.frame = CGRectMake(0, self.view.frame.size.height-260,self.view.frame.size.width, 260);
}completion:^(BOOL finished){}];
}else if(textField.tag == 52){
if ([[(UITextField*)[self.view viewWithTag:51] text] isEqualToString:@""]) {
[[[UIAlertView alloc] initWithTitle:@"Alert" message:@"Set Region First" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil] show];
}else {
selectionVal = 2;
[UIView animateWithDuration:0.5 delay:0.1 options:UIViewAnimationOptionCurveEaseIn animations:^{
_viewForPicker.frame = CGRectMake(0, self.view.frame.size.height-260,self.view.frame.size.width, 260);
}completion:^(BOOL finished){}];
}
}else if (textField.tag == 53){
if ([[(UITextField*)[self.view viewWithTag:51] text] isEqualToString:@""]) {
[[[UIAlertView alloc] initWithTitle:@"Alert" message:@"Set Region First" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil] show];
}else{
selectionVal = 3;
[UIView animateWithDuration:0.5 delay:0.1 options:UIViewAnimationOptionCurveEaseIn animations:^{
_viewForPicker.frame = CGRectMake(0, self.view.frame.size.height-260,self.view.frame.size.width, 260);
}completion:^(BOOL finished){}];
}
}
[_regionPicker reloadAllComponents];
return NO;
}

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

- (NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component{
if(selectionVal == 1){
return [regionArray count];
}else if(selectionVal == 2){
if ([[(UITextField*)[self.view viewWithTag:51] text] isEqualToString:@"Chennai-south"]) {
return chennaiSouthArray.count;
}else if ([[(UITextField*)[self.view viewWithTag:51] text] isEqualToString:@"Chennai-north"]){
return chennaiNorthArray.count;
}else if ([[(UITextField*)[self.view viewWithTag:51] text] isEqualToString:@"Villupuram"]){
return villupuramArray.count;
}
}else if(selectionVal == 3){
}
return 0;
}

- (NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
if(selectionVal == 1){
return [regionArray objectAtIndex:row];
}else if(selectionVal == 2){
if ([[(UITextField*)[self.view viewWithTag:51] text] isEqualToString:@"Chennai-south"]) {
return [chennaiSouthArray objectAtIndex:row];
}else if ([[(UITextField*)[self.view viewWithTag:51] text] isEqualToString:@"Chennai-north"]){
return [chennaiNorthArray objectAtIndex:row];
}else if ([[(UITextField*)[self.view viewWithTag:51] text] isEqualToString:@"Villupuram"]){
return [villupuramArray objectAtIndex:row];
}
}
return @"test";
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
if(selectionVal == 1){
[(UITextField*)[self.view viewWithTag:51] setText:[regionArray objectAtIndex:row]];
}else if(selectionVal == 2){
if ([[(UITextField*)[self.view viewWithTag:51] text] isEqualToString:@"Chennai-south"]) {
[(UITextField*)[self.view viewWithTag:52] setText:[chennaiSouthArray objectAtIndex:row]];
}else if ([[(UITextField*)[self.view viewWithTag:51] text] isEqualToString:@"Chennai-north"]){
[(UITextField*)[self.view viewWithTag:52] setText:[chennaiNorthArray objectAtIndex:row]];
}else if ([[(UITextField*)[self.view viewWithTag:51] text] isEqualToString:@"Villupuram"]){
[(UITextField*)[self.view viewWithTag:53] setText:[villupuramArray objectAtIndex:row]];
}

}
}

希望对您有帮助。

关于ios - 如何在iOS中使用TextField显示基于先前PickerView的PickerView值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34926201/

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