iosuipickerviewuipickerviewdatasourceuipickerviewdelegate

Generic or Single uipickerview to show in multiple viewControllers


I want to know how can i make a generic/single UIPickerView to display in multiple ViewControllers.

I want to show a pickerView in multiple ViewController. Now how can i create it.

I know how to create it like implement and write its delegate for it..

But i don't want to create these delegate in each class and write those method again and agian..

What have i done..

I have created a new File and inherit it with UIPickerView. and create an NSArray property to show in it. and also implemented its delegate but now onward don't know how to do it .

Show me some sort of example to make a generic UIPickerView or help me creating it..


Solution

  • One way to do this would be to create a class with a completion handler that creates the picker and acts as its data source and delegate. For example, in the .h file, you could do something like this,

    typedef void(^completionHandler) (NSString *selectedString);
    
    @interface RDPickerController : NSObject <UIPickerViewDataSource, UIPickerViewDelegate>
    
    @property (strong,nonatomic) UIPickerView *picker;
    
    -(instancetype)initWithCompletionHandler:(completionHandler) completion;
    

    In the .m file, implement any of the data source and delegate methods that you need, and call the completion block in the pickerView:didSelectRow:inComponent: method,

    @interface RDPickerController ()
    @property (strong,nonatomic) NSArray *data;
    @property (copy,nonatomic) completionHandler compBlock;
    @end
    
    @implementation RDPickerController
    
    
    -(instancetype)initWithCompletionHandler:(completionHandler)completion {
        if (self = [super init]) {
            _picker = [UIPickerView new];
            _picker.delegate = self;
            _picker.dataSource = self;
            _data = @[@"One", @"Two", @"Three", @"Four", @"Five", @"Six", @"Seven", @"Eight", @"Nine", @"Ten"];
            _compBlock = completion;
        }
        return self;
    }
    
    
    -(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
        return 1;
    }
    
    
    -(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
        return self.data.count;
    }
    
    
    -(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
        return self.data[row];
    }
    
    
    -(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
        self.compBlock(self.data[row]);
    }
    

    In your controller classes, you can create and use the picker like this,

    @interface ViewController ()
    @property (strong,nonatomic) RDPickerController *pc;
    @end
    
    @implementation ViewController
    
    - (IBAction)showPicker:(UIButton *)sender {
        self.pc = [[RDPickerController alloc] initWithCompletionHandler:^(NSString *selectedString) {
            NSLog(@"%@",selectedString);
            // do whatever with the returned data here
        }];
    
        [self.view addSubview:self.pc.picker];
    }