Search code examples
iphonexcodeuitextfielduipickerview

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:CGRectMake(0, 244, 320, 270)];

[self.view addSubview:picker];


return NO;
}

Solution

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