【发布时间】:2016-01-21 14:17:21
【问题描述】:
我有三个文本字段regionTextsectionTextdistributionText。如果我单击第一个文本字段 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 值?
【问题讨论】:
标签: ios objective-c uitextfield uipickerview