从多个UITextField中选择多个UIPickerView

时间:2014-09-16 00:04:44

标签: ios objective-c

我想创建一个表单并有多个uitextfields,当选中时会显示不同的uipickerviews。我的代码适用于一个uipickerview,但我如何使它适用于多个选择器视图。

这是我的代码:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Date Field
    UIDatePicker *datePicker = [[UIDatePicker alloc]init];
    //[datePicker setDate:[NSDate date]];
    datePicker.datePickerMode = UIDatePickerModeDate;
    [datePicker addTarget:self action:@selector(updateTextField:) forControlEvents:UIControlEventValueChanged];
    [self.dateField setInputView:datePicker];
    [datePicker addTarget:self action:@selector(updateTextField:) forControlEvents:UIControlEventValueChanged];

    // Account Field
    //Create Array of Accounts
    accountArray = [[NSMutableArray alloc]init];
    objectArray = [[NSMutableArray alloc]init];
    PFQuery *query = [PFQuery queryWithClassName:@"Account"];
    [query whereKey:@"user" equalTo:[PFUser currentUser]];
    [query orderByDescending:@"startingBalance"];
    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {

        for (PFObject *object in objects) {
            [accountArray addObject:object[@"name"]];
            [objectArray addObject:[object objectId]];
            //NSLog(@"%@", [object objectId]);
        }
        accountPicker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 50, 100, 150)];
        //NSLog(@"%@", accountArray[0]);
        accountPicker.showsSelectionIndicator = YES;
        self.accountField.inputView = accountPicker;
        for (NSString *obj in self.objectArray) {
            NSLog(@"Inside string %@",obj);
        }
        accountPicker.delegate = self;
    }];

    //Category Field
    //Create Array of Accounts
    categoryArray = [[NSMutableArray alloc]init];
    categoryObjectArray = [[NSMutableArray alloc]init];
    PFQuery *queryCategory = [PFQuery queryWithClassName:@"Category"];
    [queryCategory whereKey:@"user" equalTo:[PFUser currentUser]];
    [queryCategory orderByDescending:@"name"];
    [queryCategory findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {

        for (PFObject *object in objects) {
            [categoryArray addObject:object[@"name"]];
            [categoryObjectArray addObject:[object objectId]];
            //NSLog(@"%@", [object objectId]);
        }
        categoryPicker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 50, 100, 150)];
        //NSLog(@"%@", accountArray[0]);
        categoryPicker.showsSelectionIndicator = YES;
        self.categoryField.inputView = categoryPicker;
        for (NSString *obj in self.categoryObjectArray) {
            NSLog(@"Inside string %@",obj);
        }
        categoryPicker.delegate = self;
    }];


    for (NSString *obj in self.accountArray) {
        NSLog(@"Outside Block%@",obj);
    }
}

- (NSInteger)numberOfComponentsInPickerView:
(UIPickerView *)pickerView
{
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView
numberOfRowsInComponent:(NSInteger)component
{
    return accountArray.count;
}

- (NSString *)pickerView:(UIPickerView *)pickerView
             titleForRow:(NSInteger)row
            forComponent:(NSInteger)component
{
    return accountArray[row];
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    accountField.text = [accountArray objectAtIndex:row];
    number = [objectArray objectAtIndex:row];
}

1 个答案:

答案 0 :(得分:0)

This answer should help you out.它和你几乎一样的问题。您使用TextFielđelegate方法