gpt4 book ai didi

ios - 如何根据标签显示 UIPickerView 的选定值

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:08:23 25 4
gpt4 key购买 nike

我正在尝试根据 View 中标签的值显示具有选定值的 UIPickerView。

例如,如果标签值为 2,当我打开 pickerView 时,我希望在选取器中选择值 2。怎么会这样?

我的代码:

-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{

//res is array
return [[res objectAtIndex:row]objectForKey:@"choice_name"];
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
//choiceList is sting
self.choiceList = [NSString stringWithFormat:@"<r_PM act='closepos_choice' cl_choice='%d'/>", row];
}

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {

UILabel *pickerLabel = (UILabel *)view;
if (pickerLabel == nil) {
CGRect frame = CGRectMake(0.0, 0.0, 300, 30);
pickerLabel = [[[UILabel alloc] initWithFrame:frame] autorelease];
[pickerLabel setTextAlignment:UITextAlignmentCenter];
[pickerLabel setBackgroundColor:[UIColor clearColor]];
[pickerLabel setFont:[UIFont fontWithName:@"HelveticaNeue" size:16.0]];

//EDITED CODE FOR C_X
NSNumberFormatter * f = [[NSNumberFormatter alloc] init];
[f setNumberStyle:NSNumberFormatterDecimalStyle];
NSNumber * myNumber = [f numberFromString:lblOtomatikPozKap.text];
[f release];

NSInteger index = [res indexOfObject:myNumber];

if(index != NSNotFound ){

[self.pickerView selectRow:index inComponent:0 animated:NO];
}

//EDITED FOR AUSTIN
NSInteger indexOfItem = [res indexOfObjectPassingTest:^BOOL(id obj, NSUInteger idx, BOOL *stop) {
return [[obj objectForKey:@"choice_name"] isEqualToString:@"2"];
}];

if (indexOfItem != NSNotFound)
{
[self.pickerView selectRow:indexOfItem inComponent:1 animated:NO];
}
}

NSString *str = [NSString stringWithFormat:@"%d- %@", row, [[res objectAtIndex:row]objectForKey:@"choice_name"]];

[pickerLabel setText:str];

return pickerLabel;
}

//Not working at all
- (void)selectRow:(UIPickerView*)row inComponent:(NSInteger)component animated:(BOOL)animated
{}

最佳答案

您可以通过在 pickerview selectRow:inComponent:animated 上调用此方法来选择一行:编辑:

您正在从 res 获取字符串,首先您必须将标签字符串添加到此数组,以便您可以在 pickerView 中显示此行。 之后,在 viewWillAppear 或您想要调用此方法的任何其他方法中。首先获取该字符串/对象的索引。

NSInteger index=[resindexOfObject:label.text];

现在选择行。

if(index != NSNotFound ){
[self.myPicker selectRow:index inComponent:0 animated:NO]
}

关于ios - 如何根据标签显示 UIPickerView 的选定值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21115170/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com