Zach Starnes
Zach Starnes

Reputation: 3198

Getting data from programmatically created UIPickerView

I have a UIPickerView that was programmed manually so that I was able to add seconds. The UIPickerView lets you choose hours, minutes, and seconds. I am having a very hard time figuring out how to get the data from this picker so that I can store it. Can someone help me? All the ones that apple has pre made have attributes that you can easily call to get the value but since I am using my own I am not sure how to tackle it. Thanks!

my didSelectRow method

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    NSInteger hRow = [timerDurationPicker selectedRowInComponent:0];
    NSInteger mRow = [timerDurationPicker selectedRowInComponent:1];
    NSInteger sRow = [timerDurationPicker selectedRowInComponent:2];
}

Upvotes: 0

Views: 748

Answers (3)

iPatel
iPatel

Reputation: 47049

If you want to get selected row after scrolling of UIPickerView is over. Then you need to add valueChanged event of UIPickerView.

[myPickerView addTarget:self action:@selector(valueChange:) forControlEvents:UIControlEventValueChanged];

And add method name is

-(void)valueChange:(UIPickerView *) picker
{
  NSLog (@"%@", [picker selectRow:0 inComponent:0 animated:YES]); // set row and component value.
}

pickerView:didSelectRow:inComponent is a delegate method that return selected row in component, here you need to set your row and component.

EDITE:

Try with following code

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

    int hRow = [[arrayOfHour objectAtIndex:[timerDurationPicker selectedRowInComponent:0]] intValue];
    int mRow = [[arrayOfMinute objectAtIndex:[timerDurationPicker selectedRowInComponent:1]] intValue];
    int sRow = [[arrayOfSecond objectAtIndex:[timerDurationPicker selectedRowInComponent:2]] intValue];

    NSLog (@"%d", hRow);
    NSLog (@"%d", mRow);
    NSLog (@"%d", sRow);
}

Just put my suggestion - Use UIDatePicker (with time Mode) instead of using UIPickerView, It is very easy to manage, you does not need to take/manage array of hour, minute and second.

Upvotes: 1

Mani
Mani

Reputation: 17585

Then you can implement this delegate method.

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
      //Do you stuff here.
}

It will fire when you select on the particular row in picker. For example, If you select on first component, You can get selected index for first. At the same time, you want to get index of other component means, you can use this method. selectedRowInComponent:.

Upvotes: 1

Natarajan
Natarajan

Reputation: 3271

You can assign the delegate and datasource for the UIPickerView. So, In delegate methods you can get the actions from UIPickerView. Please refer this for more info : https://developer.apple.com/library/ios/documentation/uikit/reference/UIPickerView_Class/Reference/UIPickerView.html

Selecting Rows in the View Picker

  • – selectRow:inComponent:animated:
  • – selectedRowInComponent:

Upvotes: 0

Related Questions