Я создал два UITextFields
и хочу создать два разных UIPickerViews
для обоих textFields
с различными значениями и как составить карту textFields
для каждого pickerView
? любая помощь?Как создать два представления сборщика для двух текстовых полей?
-1
A
ответ
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";
}
1
как насчет textField.inputview
, чтобы выбрать вариант соответственно для этого.
textField.inputView = pickerView1;
textField1.inputView = pickerView2;
Если я пишу код в этой ситуации, то вместо создания двух pickerView
, я создать один pickerview
и изменить datasource
для каждого textfield
0
textField1.inputView = pickerView1;
textField2.inputView = pickerView2;
pickerView.tag = 1;
pickerView.tag = 2;
Использование UIPickerView
методов делегата с тегами.
Смежные вопросы
- 1. Два сборщика для 2 текстовых полей в одном представлении
- 2. Сравнение двух текстовых полей
- 3. соответствующие значения двух текстовых полей
- 4. Проверка Jquery-проверки для двух текстовых полей
- 5. Первый ответчик двух текстовых полей
- 6. Позиционирование двух смежных текстовых полей
- 7. Создать новый пользователь и его сравнить для двух текстовых полей
- 8. Как вы добавляете содержимое двух текстовых полей?
- 9. Программное добавление двух текстовых полей в таблицуViewCell
- 10. автозаполнение для двух текстовых полей jQuery mobile
- 11. Как использовать один выбор даты для двух текстовых полей?
- 12. Как синхронизировать прокрутку двух многострочных текстовых полей?
- 13. Как установить Dynamically fontsize для двух текстовых полей в wpf
- 14. Двойная прокрутка двух богатых текстовых полей
- 15. Объединение двух многострочных текстовых полей в VB.NET
- 16. место двух текстовых полей рядом в jqgrid
- 17. Сохранение двух текстовых полей в Firebase
- 18. Objective-C loop для сравнения двух текстовых полей для паролей
- 19. Добавление/Объединение двух текстовых полей в одну метку в iOS
- 20. При использовании представления сборщика
- 21. C# WinForms - добавление двух текстовых полей автоматически
- 22. Как создать прокручиваемый список текстовых полей?
- 23. Как создать массив текстовых полей в JavaFX
- 24. Как создать список текстовых полей в JavaScript?
- 25. Обработка действия отправки двух текстовых полей
- 26. Сохранение текстовых полей табличного представления в массив настраиваемых объектов
- 27. jQuery как удалить удаленный элемент для удаления двух текстовых полей
- 28. Как создать несколько текстовых полей в Carbon?
- 29. Номера Ipad только в одном из двух текстовых полей
- 30. Использование одного и того же UIDatePicker для двух текстовых полей