简体   繁体   中英

Need Help Setting UIPicker's Label From Selection

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 ]];
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM