Question

I have this code to add a datepicker:

// Initiate datepicker and assign to due date
_datePicker = [[UIDatePicker alloc] init];
[_datePicker addTarget:self action:@selector(dateChanged:)
 forControlEvents:UIControlEventValueChanged];
_datePicker.minuteInterval = 15;
_dueDate.inputView = _datePicker;

I tried to do the same thing with a pickerview but I had no success, could somebody explain why?

Here is my code: // Initaite the pickerview and assign to priority text field _priorityPickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 200, 320, 200)];

// Array otions for priority picker
NSArray *array = [[NSArray alloc] initWithObjects:@"High",@"Medium",@"Low", nil];
self.priorityPickerData = array;

// Connect text field to pickerview
_priority.inputView = _priorityPickerView;

Here is the data set in the picker view:

// Picker View functions
-(NSInteger)numberOfComponantsInPickerView:(UIPickerView *)pickerView{
    return 1;
}

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

-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    return [self.priorityPickerData objectAtIndex:row];

}

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    int select = row;
    if(select == 0){
        _priority.text = @"High";
    }else if(select == 1){
        _priority.text = @"Medium";
    }else if(select == 2){
        _priority.text = @"Low";
    }
}

Many thanks in advance,

Peter

Was it helpful?

Solution

Set the delegate and datasource to self:

_priorityPickerView.delegate = self;
_priorityPickerView.dataSource = self;

Also at the top of your .m file:

@interface YourViewController ()<UIPickerViewDataSource,UIPickerViewDelegate>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top