[英]Need Help Setting UIPicker's Label From Selection
I have a UIPicker with a UILabel above it that updates displaying the current selection.我有一个 UIPicker 上面有一个 UILabel ,它更新显示当前选择。
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.当我 go 第二次查看此视图时,UIPicker 的选择位于我离开的最后一个单元格上,但 label 位于默认字符串上。 How can I make it so the label is also on where I last left off?
我怎样才能使 label 也在我上次离开的地方?
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";
).设置默认的 numberLabel 文本值(如
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)displayPicker ,将视图呈现给用户,但是
- (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:您可能希望通过插入一些
NSLog()
语句来检查此类行为,并可能使用以下内容进行更正:
- (void)displayPicker
{
self.numberLabel.text = [NSString stringWithFormat: @"%i number",
[self.numberPicker selectedRowInComponent: 0 ]];
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.