Need Help Setting UIPicker's Label From Selection - iphone

I have a UIPicker with a UILabel above it that updates displaying the current selection.
When I go to this view the 2nd time, the UIPicker's selection is on the last cell where I left off, but the label is on the default string. How can I make it so the label is also on where I last left off?
This is the code I am using:
- (void)displayPicker
{
self.numberLabel.text = #"1 number";
}
- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
self.numberButtonText = [NSString stringWithFormat:#"%# number", [self.pickerArray objectAtIndex:row]];
self.numberLabel.text = self.numberButtonText;
}

If i assume correct, you are calling
-(void)displayPicker
to set the default numberLabel text value (as in self.numberLabel.text = #"1 number";).
Possible cause would be,
that - (void)displayPicker is called every time, the view is presented to the user, but
- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
is not.
You might want to check for such behavior, by inserting some NSLog() statements and perhaps correct it by using something like this:
- (void)displayPicker
{
self.numberLabel.text = [NSString stringWithFormat: #"%i number",
[self.numberPicker selectedRowInComponent: 0 ]];
}

Related

How to show previous selected value of a uipickerview in ios programming

How to show previously selected value of a UIPickerView in iOS programming
You have to take two variable for it 1) currentSelectedIndex 2)prevSelectedIndex
And In
- (void)pickerView:(UIPickerView *)pV didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
prevSelectedIndex = currentSelectedIndex
currentSelectedIndex = row;
//your Code...
}
now you can use prevSelectedIndex to get pickerDataSourceArray value which was previously selected.

Set UITextField's text as default value in UIPickerView in iPhone.?

I have some text in textField on view load.I want that particular text to be set as default value of UIPickerView ..The value in textField is one of the value of UIPickerView..How can I make it possible.For the code I m writing now is always first value is selected as default value in UIPickerView.But I want the text of textField to be set as default value in PickerView.
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
txtstate.text= [[arr objectAtIndex:row] valueForKey:#"Code"];
}
How can I do it ?
uint selectedRow = [pickerView selectedRowInComponent:0];
NSString *title = [[pickerView delegate] pickerView:myPickerView titleForRow:selectedRow inComponent:0];
If you have more than a single component, adjust accordingly.

UIPickerView problems

I have a very simple application that use a 2 component UIPickerView that causes me a crash every time I click over it. I dragged it into my view by IB, then hooked up dataSource and delegate to File's Owner. In the .h file:
#interface SettingsViewController : UIViewController <UIPickerViewDataSource, UIPickerViewDelegate> {
While in .m
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)thePickerView {
return 2;
}
- (NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component {
NSInteger value;
if (component == 0) {
value = [tipiDado count];
} else {
value = [numeroDadi count];
}
return value;
}
- (NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
if (component == 0) {
return [tipiDado objectAtIndex:row];
} else {
return [numeroDadi objectAtIndex:row];
}
}
- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
NSLog(#"Selected Dice: %#. Number of Dice: %#", [tipiDado objectAtIndex:row], [numeroDadi objectAtIndex:row]);
}
I dunno why it continues to give me SIGBART or EXC_BAD_ACCESS... I don't know where I'm doing wrong.
Suggestions?
Thanks folks.
It's difficult to answer properly without seeing some more code. When it crashes, you should be able to see exactly what line is causing the crash (look at the call stack on the left). My guess is that either one of your arrays (tipiDado or numeroDadi) are not retained properly or else that the objects stored in them are not of type NSString.
If you update the question with the code you use to initialize them, it will be easier to point out the exact problem.
It's probably because your arrays (tipiDado and numeroDadi) are no longer valid. Maybe they are set up as autoreleased objects?
What you can do, is start in debug mode (debug configuration and with debugger attached) and it should stop right at the line where it crashes.
Try using Allocations with zombo detections / reference counter to see which object is the problem.
In didSelectRow, you are using the same row value to access both arrays. If the arrays are not the same size, you could be accessing an out-of-range item.
You should either check one array only based on the component parameter or to show both selections you can do this instead:
NSInteger tipiDadoRow = [thePickerView selectedRowInComponent:0];
NSInteger numeroDadiRow = [thePickerView selectedRowInComponent:1];
NSLog(#"Selected Dice: %#. Number of Dice: %#",
[tipiDado objectAtIndex:tipiDadoRow], [numeroDadi objectAtIndex:numeroDadiRow]);
I used property/synthesize to initialize them, so when I filled them up with [NSArray arrayWithObjects:...] I haven't added retain but I forget to use self. notation!!
Writing down:
self.tipiDado = [NSArray arrayWithObjects:#"D4",...];
fixed up he problem.

label not updating with picker view

I have a pickerview pulling from a datasource. I have code to update a label in the didSelectRow function, but the label is not updating. When I print the value to the NSLog, the proper value is printed. Is there something special I need to do to hookup the label so that it updates when the didSelectRow is eneter?
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
if (pickerView.tag == TagLensPicker){
[self lensArrayData];
label.text = [NSString stringWithFormat:#"%#",[description objectAtIndex:[pickerView selectedRowInComponent:0]]];
NSLog([NSString stringWithFormat:#"%#", [description objectAtIndex:[pickerView selectedRowInComponent:0]]]);
}
}
[pickerView selectedRowInComponent:0]
might be the source of your problem.
[description objectAtIndex:row] should work
If NSLog prints the correct value, there must be a problem with the label variable.
Print it using NSLog, see if it's a correct reference to your label?
Also, you may try calling [label setNeedsDisplay] after changing the text, although I'm not sure it's necessary.

How does UIPickerView handle 2+ dials spinning?

If I have a UIPickerView with three components (dials). How does the picker handle two dials spinning simultaneously? For example, the user might flick the first dial, which spins freely and immediately slowly click to a selection on the second dial.
I'm doing the following in the picker. If one dial is spinning, I don't capture its value. I only capture values when the dials spin one at a time.
- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
if(component == 0){
dateEndCenturyText = (NSString *)[dateCentury objectAtIndex:row];
}
else if(component == 1){
dateEndDecadeText = (NSString *)[dateYear objectAtIndex:row];
}
else if(component == 2){
dateEndYearText = (NSString *)[dateYear objectAtIndex:row];
}
Is there a better way to ensure capture of values even if two dials are spinning?
The most robust solution to this may be to update all three values each time your event fires. You can retrieve the selected row for any component in the UIPickerView using the following method:
UIPickerView selectedRowInComponent:
Returns the index of the selected row in a given component
- (NSInteger)selectedRowInComponent:(NSInteger)component
Hope this helps!