#import "ViewController.h"
@interface ViewController ()<UIPickerViewDelegate,UIPickerViewDataSource>
{
int selectedTextfieldValue;
NSArray *namesArray , *genderArray, *ageArray;
UIToolbar *pickerToolbar;
}
@end
@implementation ViewController
@synthesize myPickerView;
-(void)viewDidLoad
{
[super viewDidLoad];
namesArray=[[NSArray alloc]initWithObjects:@"a",@"b", nil];
genderArray=[[NSArray alloc]initWithObjects:@"M",@"F", nil];
ageArray=[[NSArray alloc]initWithObjects:@"26",@"25", nil];
selectedTextfieldValue=0;
}
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
[self.view endEditing:YES];
if (textField==_text1)
{
_text1.delegate=self;
}
else
if(textField==_text2)
{
_text2.delegate=self;
}
else
{
_text3.delegate=self;
}
NSLog(@"%d",textField.tag);
selectedTextfieldValue=(int)textField.tag;
NSLog(@"%d",selectedTextfieldValue);
// call picker here according to textfield tag you can set text to textfiled
[self popoverWithInformation];
}
-(void)popoverWithInformation
{
pickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 220, 44)];
pickerToolbar.barStyle = UIBarStyleBlackOpaque;
[pickerToolbar sizeToFit];
NSMutableArray *barItems = [[NSMutableArray alloc] init];
UIBarButtonItem *cancelBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(pickerCancel:)];
[barItems addObject:cancelBtn];
/ * UIBarButtonItem flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil]; [barItems addObject:flexSpace]; /
UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(pickerDone:)];
[barItems addObject:doneBtn];
[pickerToolbar setItems:barItems animated:YES];
myPickerView = [[UIPickerView alloc] init];
myPickerView.showsSelectionIndicator = YES;
CGRect pickerRect = myPickerView.bounds;
myPickerView.bounds = pickerRect;
myPickerView.frame = CGRectMake(0, 44, 320, 216);
UIView* popoverView = [[UIView alloc] initWithFrame:CGRectMake(0, 44, 320, 300)];
popoverView.backgroundColor = [UIColor whiteColor];
[popoverView addSubview:myPickerView];
[popoverView addSubview:pickerToolbar];
[self.view addSubview:popoverView];
}
// tell the picker how many rows are available for a given component
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent: (NSInteger)component
{
int count = 0;
switch (selectedTextfieldValue)
{
case 1:
count =namesArray.count;
NSLog(@"%d",count);
break;
case 2:
count =genderArray.count;
NSLog(@"%d",count);
break;
case 3:
count =ageArray.count;
NSLog(@"%d",count);
break;
default:
NSLog(@"default in count");
break;
}
return count;
}
// tell the picker how many components it will have
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 1;
}
// tell the picker the title for a given component
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
NSString *title;
switch (selectedTextfieldValue) {
case 1:
title =namesArray[row];
break;
case 2:
title =genderArray[row];
break;
case 3:
title =ageArray[row];
break;
default:
NSLog(@"default");
break;
}
return title;
}
// tell the picker the width of each row for a given component
-(CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
int sectionWidth = 300;
return sectionWidth;
}
-(void)pickerDone:(id)sender
{
NSLog(@"Done Clicked");
NSLog(@"Done Clicked%d",selectedTextfieldValue);
myPickerView.hidden=YES;
pickerToolbar.hidden=YES;
}
-(void)pickerCancel:(id)sender
{
NSLog(@"cancel");
myPickerView.hidden=YES;
pickerToolbar.hidden=YES;
}
@end
我想在选择器视图中显示数组的值,为此我需要3个数组和3个文本字段但是选择器视图没有显示数据。 任何帮助都将受到高度赞赏。
答案 0 :(得分:0)
在此,您不需要每次都制作PickerView。每次进行第一次pickerview并重新加载组件
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
[self.view endEditing:YES];
if (textField==_text1)
{
_text1.delegate=self;
} else if(textField==_text2) {
_text2.delegate=self;
} else {
_text3.delegate=self;
}
selectedTextfieldValue=(int)textField.tag;
[myPickerView reloadAllComponents];
}
可能会帮助其他明智的访问UIPickerView programatic&amp; UIPickerView Tutorial