我已经创建了两个UITextFields
,并且想为具有不同值UIPickerViews
的两个创建两个不同的,textFields
以及如何将个人具体映射textFields
到每个pickerView
?有什么帮助吗?
问问题
467 次
3 回答
1
怎么样textField.inputview
,像这样分别为您选择选项。
textField.inputView = pickerView1;
textField1.inputView = pickerView2;
如果我在这种情况下编写代码,那么pickerView
我不会创建两个,而是创建一个pickerview
并datasource
为每个更改textfield
于 2013-02-19T14:57:45.823 回答
0
首先创建两个数组,其中包含要在差异选择器中显示的不同值,我们说 aray1 和 ary2 。
现在将其添加到 .h 文件中
@interface YourViewController : UIViewController<UITextFieldDelegate,UIPickerViewDelegate, UIPickerViewDataSource>
{
UIToolbar* keyboardDoneButtonView;
UIPickerView *pickerView;
NSString * txtFieldSelected;
NSArray *ary2;
NSArray *ary1;
}
在 .h 文件中
- (void)viewDidLoad
{
[super viewDidLoad];
firstTF.delegate = self;
secondTF.delegate = self;
ary1 =[NSArray arrayWithObjects:@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8", nil];
ary2 =[NSArray arrayWithObjects:@"a",@"s",@"d",@"f",@"g",@"h",@"x",@"c", nil];
}
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
if (textField == firstTF) {
txtFieldSelected = @"firstTF";
}else if (textField == secondFF) {
txtFieldSelected = @"secondTF";
}
CGRect pickerFrame = CGRectMake(0, 40, 0, 0);
pickerView = [[UIPickerView alloc] initWithFrame:pickerFrame];
pickerView.showsSelectionIndicator = YES;
pickerView.dataSource = self;
pickerView.delegate = self;
keyboardDoneButtonView = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
keyboardDoneButtonView.barStyle = UIBarStyleBlackTranslucent;
[keyboardDoneButtonView sizeToFit];
NSMutableArray *barItems = [[NSMutableArray alloc] init];
UIBarButtonItem *cancelBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancelButtonPressed:)];
[barItems addObject:cancelBtn];
UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
[barItems addObject:flexSpace];
UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(doneButtonPressed:)];
[barItems addObject:doneBtn];
[keyboardDoneButtonView setItems:barItems animated:YES];
textField.inputAccessoryView = keyboardDoneButtonView;
textField.inputView = pickerView;
return YES;
}
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component;
{
if (txtFieldSelected == @"firstTF") {
return ary1.count;
}else if (txtFieldSelected == @"secondTF") {
return ary2.count;
}
return 1;
}
-(void)doneButtonPressed:(id)sender{
if (txtFieldSelected == @"firstTF") {
[firstTF resignFirstResponder];
}else if (txtFieldSelected == @"secondTF") {
[secondTF resignFirstResponder];
}
}
-(void)cancelButtonPressed:(id)sender{
if (txtFieldSelected == @"firstTF") {
[firstTF resignFirstResponder];
}else if (txtFieldSelected == @"secondTF") {
[secondTF resignFirstResponder];
}
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow: (NSInteger)row inComponent:(NSInteger)component {
// called when a row is selected
if (txtFieldSelected == @"firstTF") {
firstTF.text = [ary1 objectAtIndex:row];
}else if (txtFieldSelected == @"secondTF") {
secondTF.text = [ary2 objectAtIndex:row];
}
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
if (txtFieldSelected == @"firstTF") {
return [ary1 objectAtIndex:row];
}else if (txtFieldSelected == @"secondTF") {
return [ary2 objectAtIndex:row];
}
return @"Good Day";
}
于 2013-02-19T15:00:53.260 回答
0
textField1.inputView = pickerView1;
textField2.inputView = pickerView2;
pickerView.tag = 1;
pickerView.tag = 2;
使用UIPickerView
带标签的委托方法。
于 2013-02-19T15:35:01.113 回答