如何在iPhone中将文本字段值设置为选择器?

时间:2012-10-25 11:48:21

标签: iphone objective-c

我有一个应用程序,其中我有一个文本字段,我在其中显示数据库中的值。文本字段旁边有一个按钮。我有选择器,其中包含4个值,即自定义,步行,跑步,慢跑。

如果textfield包含一个值,即如果包含'Walk',那么在点击按钮时我会显示其中填充了4个值的选择器。但我的问题是,当点击按钮时,应该在选择器上设置值'Walk'。

这是我的代码。

- (void)viewDidLoad
{
    [super viewDidLoad];
    currentarray = [[NSMutableArray alloc] initWithObjects:@"Custom",@"Walk",@"Run",@"Jog",nil];
       // Do any additional setup after loading the view from its nib.
}

这是我的按钮操作,单击此按钮将打开选择器:

-(IBAction)choose
{
    actionsheet = [[UIActionSheet alloc]initWithTitle:[currentarray objectAtIndex:0] delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil, nil];

    [actionsheet setTitle:@"Picker"];

    pickerView = [[UIPickerView alloc]initWithFrame:CGRectMake(0, 40, 0, 0)];
    pickerView.dataSource=self;
    pickerView.delegate=self;
    pickerView.showsSelectionIndicator=YES;
    [actionsheet addSubview:pickerView];
    [actionsheet showInView:self.view];
    [pickerView selectRow:[txtTextfield.text intValue ]-1 inComponent:0 animated:YES];        
    [actionsheet showInView:[[UIApplication sharedApplication]keyWindow]];
    [actionsheet setBounds:CGRectMake(0, 0, 320, 485)];        
}

-(NSString*)pickerView:(UIPickerView*)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{      
    NSString *string;
    string = [NSString stringWithFormat:@"%@",[currentarray objectAtIndex:row]];
    return string;      
}

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

-(NSInteger)pickerView:(UIPickerView*)pickerView numberOfRowsInComponent:(NSInteger)component{
    return [currentarray count];
}

1 个答案:

答案 0 :(得分:0)

您可以像这样设置pickerView,

 [pickerView setObject:textField.text];

查看以下链接,

http://iphonedevsdk.com/forum/iphone-sdk-development/11594-set-selected-row-of-uipickerview-in-applicationdidfinishlaunching.html