繁体   English   中英

在uipickerview中依次选择来编辑2 uitextfield

[英]Edit 2 uitextfield by turn with a select in uipickerview

我有2个UITextField实例和1个UIPickerView实例。 UIPickerView能够将数据更新到didSelectRow:(NSInteger)row inComponent:(NSInteger)component UITextField中。 但是,当用户触摸UITextView时,如何从UIPickerView更新2 UITextField

谢谢。 :)

您将需要使UIPickerView为文本字段的inputView,我已经举了一个例子。

.h文件:

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

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

@end

.m需要以下内容:

- (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;
}

我还没有包含任何清理代码。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM