gpt4 book ai didi

ios - UIPicker selectRow 不工作-这段代码有什么问题?

转载 作者:可可西里 更新时间:2023-11-01 03:06:31 25 4
gpt4 key购买 nike

我正在尝试为 UIPickerView 设置一个默认值。但是,无论我将其设置为哪个值,它始终默认为 1。下面的方法可以很好地加载选择器,我可以选择它。那么,我做错了什么?这是代码。

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.

arrayNo = [[NSMutableArray alloc] init];
[arrayNo addObject:@" 1 "];
[arrayNo addObject:@" 2 "];
[arrayNo addObject:@" 3 "];
[arrayNo addObject:@" 4 "];
[arrayNo addObject:@" 5 "];

[pickerView selectRow:4 inComponent:0 animated:YES];
mlabel.text= [arrayNo objectAtIndex:[pickerView selectedRowInComponent:0]];

}

Picker 的数据源方法:

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

mlabel.text = [arrayNo objectAtIndex:row];
NSNumberFormatter * f = [[NSNumberFormatter alloc] init];

[f setNumberStyle:NSNumberFormatterDecimalStyle];

NSNumber * myNumber = [f numberFromString:[arrayNo objectAtIndex:row]];

[f release];

prefs = [NSUserDefaults standardUserDefaults];

NSInteger myInteger = [myNumber integerValue];


// save level
[prefs setInteger:myInteger forKey:@"myInt"];

// saving it all
[prefs synchronize];


}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component;
{
return [arrayNo count];
}

最佳答案

如果我把它放在这个方法中,它会起作用:

-(void) viewWillAppear: (BOOL) animated

关于ios - UIPicker selectRow 不工作-这段代码有什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7393917/

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