我有三个TextField regionText
sectionText
distributionText
。如果我单击第一个文本字段regionText
表示,它应该显示PickerView regionPicker
,其数组值为regionArray
。如果我点击sectionText
或distributionText
而未在regionText
中选择值,则应显示提醒消息。
然后,基于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值?
答案 0 :(得分:2)
将isRangeSlected
和fieldTag
声明为类范围变量,并更新您的方法,如下所示。
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
fieldTag = textField.tag;
// Show UIPickerView
if(textField.tag == 0) {
[self showPickerView];
} else if (textField.tag == 1) {
isRangeSlected ? [self showPickerView] : [self showAlertWithText:@"Please select range"];
} else if (textField.tag == 2) {
isRangeSlected ? [self showPickerView] : [self showAlertWithText:@"Please select range"];
}
return NO;
}
-(void)showPickerView {
[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){
}];
}
-(void)showAlertWithText:(NSString *)txt {
[[[UIAlertView alloc] initWithTitle:@"Error" message:txt delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil] show];
}
将其余PickerViewDataSource
方法更新为
-(NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component
{
if(fieldTag == 0) return [regionArray count];
else if (fieldTag == 1) return [regionArray count];
else return [regionArray count];
}
-(NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
//What i have to write.Pls update.
if(fieldTag == 0) return [regionArray objectAtIndex:row];
else if (fieldTag == 1) return [regionArray objectAtIndex:row];
else return [regionArray objectAtIndex:row];
}
如果有任何小错误,您可以自行更新代码。
答案 1 :(得分:1)
以下是经过测试的更新代码:
首先在.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]];
}
}
}
我希望它会对你有所帮助。