Lior Z
Lior Z

Reputation: 668

Simple Implementation of UIPickerView in iOS

I've been looking all over for a simple implementation of a UIPickerView in order to select / display values from a defined list in iOS.

Every tutorial I found is extremely long for such a simple task (more than 3 steps for this is extremely long in my book).

Is there some kind of way to do this with very little effort like a Spinner is used in Android?

Thanks

Upvotes: 1

Views: 171

Answers (1)

matt
matt

Reputation: 534893

In this example, self.states is the "defined list" (an NSArray of NSString):

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

- (NSInteger)pickerView:(UIPickerView *)pickerView
numberOfRowsInComponent:(NSInteger)component {
    return self.states.count;
}


- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row
          forComponent:(NSInteger)component reusingView:(UIView *)view {
    UILabel* lab;
    if (view)
        lab = (UILabel*)view;
    else
        lab = [[UILabel alloc] init];
    lab.text = (self.states)[row];
    lab.backgroundColor = [UIColor clearColor];
    [lab sizeToFit];
    return lab;
}

Upvotes: 1

Related Questions