开发者

Edit 2 uitextfield by turn with a select in uipickerview

开发者 https://www.devze.com 2023-01-16 13:46 出处:网络
I have 2 instances of UITextField and 1 instance ofUIPickerV开发者_运维技巧iew. UIPickerView is able to update data into

I have 2 instances of UITextField and 1 instance of UIPickerV开发者_运维技巧iew. UIPickerView is able to update data into UITextField in didSelectRow:(NSInteger)row inComponent:(NSInteger)component. But how do I update 2 UITextField from UIPickerView when the user touch the UITextView?

Thanks. :)


you will need to make the UIPickerView the inputView of the textfields, i have made an example.

.h file:

@interface aViewController : UIViewController <UIPickerViewDelegate,UIPickerViewDataSource> {
    UITextField * textField0;
    UITextField * textField1;
    UIPickerView * pickerView;
    UIToolbar * toolBar;
    NSArray * items0;
    NSArray * items1;

}
-(void)cancel;
-(void)close;

@end

.m need something like the following:

- (void)viewDidLoad
{
    [super viewDidLoad];
    textField0  = [[UITextField alloc] initWithFrame:CGRectMake(10.f, 20.0f, 100.f, 30.f)];
    textField1 = [[UITextField alloc] initWithFrame:CGRectMake(130.f, 20.0f, 100.f, 30.f)];
    textField0.backgroundColor = [UIColor grayColor];
    textField1.backgroundColor = [UIColor grayColor];
    pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 0, 320.f, 200.f)];
    // Do any additional setup after loading the view from its nib.
    [self.view addSubview:textField0];
    [self.view addSubview:textField1];

    toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320.f, 44.f)];
    UIBarButtonItem * cancelButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancel)];
    UIBarButtonItem * doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(close)];
    toolBar.items = [NSArray arrayWithObjects:doneButton,cancelButton, nil];
    items0 = [[NSArray alloc] initWithObjects:@"dogs",@"cats",@"llamas",@"emus", nil];
    items1 = [[NSArray alloc] initWithObjects:@"bone",@"catnip",@"hay", nil];
    [doneButton release];
    [cancelButton release];



    pickerView.showsSelectionIndicator = YES;
    pickerView.delegate = self;
    pickerView.dataSource = self;

    textField1.inputView = pickerView;
    textField0.inputView = pickerView;

    textField1.inputAccessoryView = toolBar;
    textField0.inputAccessoryView = toolBar;

}
#pragma mark - actions
-(void)cancel
{
    //reset the values to the original values on cancel...
    //do that here.
    [textField0 resignFirstResponder];
    [textField1 resignFirstResponder];
}

-(void)close
{
    textField0.text = [items0 objectAtIndex:[pickerView selectedRowInComponent:0]] ;
    textField1.text = [items1 objectAtIndex:[pickerView selectedRowInComponent:1]] ;
    [textField0 resignFirstResponder];
    [textField1 resignFirstResponder];
}

#pragma mark - pickerview handling

-(float)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component{
    return 130.f;
}
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    if (component==0) {
        return [items0 objectAtIndex:row];
    }
    return [items1 objectAtIndex:row];

}
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    if (component==0) {
        textField0.text = [items0 objectAtIndex:row];
        return;
    }
    textField1.text = [items1 objectAtIndex:row];
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    if (component ==0) {
        return [items0 count];
    }
    return [items1 count];
}
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 2;
}

I haven't included any clean-up code.

0

精彩评论

暂无评论...
验证码 换一张
取 消