XCode:如何直接用内容填充代码中的UIPickerView?

时间:2011-09-07 08:37:08

标签: iphone xcode uitextfield uipickerview

我该怎么做?我想用EURO,USD,POUND等值填充它,并在点击相应的行时将值粘贴到文本字段中。

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField { 
// Make a new view, or do what you want here
UIPickerView *picker = [[UIPickerView alloc] 
                        initWithFrame:CGRectMake(0, 244, 320, 270)];

[self.view addSubview:picker];


return NO;
}

4 个答案:

答案 0 :(得分:3)

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
     myTextField.text= [PickerArray objectAtIndex:row];
}

答案 1 :(得分:2)

您应该在您的委托方法-(NSString*) pickerView:(UIPickerView*)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component中实施,该方法将为您的行返回标题@"EURO"@"USD"@"POUND"@"RUB"

例如,

-(NSString*) pickerView:(UIPickerView*)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    switch(component)
    {
        case 0:
            return @"EURO";
        case 1:
            return @"USD";
        case 2:
            return @"POUND";
        case 3:
            return @"RUB";
    }
    return @"";
}

答案 2 :(得分:0)

在委托方法中编写代码

-(NSString*) pickerView:(UIPickerView*)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component

{ 返回字符串; }

在数组中添加如下@“one”,@“two”,@“three”,@“four”

答案 3 :(得分:0)

正如其他人已经写过的那样,你必须添加上面提到的委托方法来用数组中的项填充你的PickerView。要将值写入文本字段,您可以通过两种方式执行此操作。您可以编写一个pickerView didSelectRow:委托方法,如下所示:

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component

{

myTextField.text= [myArray objectAtIndex:row];

}

每次点击一行时,这将更新textField。另一种方法是你在按下按钮或其他动作时拉取pickerView的值。与

类似
- (void)someAction:(id)sender
{

    NSInteger row = [myPicker selectedRowInComponent:0];
    myTextField.text = [myArray objectAtIndex:row];

}