开发者

XCode: How to fill UIPickerView in code directly with content?

How do I do that? i want to fill it with values like EURO, USD, POUND and so on and paste the value into a textfield when i tap on the corresponding row.

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField { 
// Make a new view, or do what you want here
UIPickerView *picker = [[UIPickerView alloc] 
                        initWithFrame:CGRectM开发者_开发问答ake(0, 244, 320, 270)];

[self.view addSubview:picker];


return NO;
}


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


You should implement in your delegate method -(NSString*) pickerView:(UIPickerView*)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component that will return titles @"EURO", @"USD", @"POUND", @"RUB" for your rows.

For example,

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


write code in your delegate method

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

{ return string; }

In Array add like this @"one", @"two", @"three", @"four"


As others have already written you have to add the above mentiond delegate method to fill up your PickerView with items from an array. For writing the value into a textfield, you can do this in 2 ways. You can either do write a pickerView didSelectRow: delegate method like this:

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

{

myTextField.text= [myArray objectAtIndex:row];

}

This will update the textField each time you tap on a row. The other way is that you pull the value of the pickerView on a button press or on some other action. Similar to

- (void)someAction:(id)sender
{

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

}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜