默认选择器值 iPhone

发布于 2024-08-09 03:38:42 字数 285 浏览 1 评论 0原文

有没有办法为选择器设置默认值?我保存所有选择器中最后选定的行,并且希望能够让选择器在启动时加载这些保存的行。截至目前,我已经找到了这段代码:

[settingsPagePicker selectRow:3 inComponent:0 animated:YES];

它有效,但仅当用户点击选择器时才有效。我需要它在应用程序首次加载时工作。如果我将此代码放在 viewDidLoad 中,应用程序将崩溃。 有人知道在我的代码中放置它的正确位置以使其工作吗?

谢谢您的宝贵时间!

Is there a way to set a default value for a picker? I save the last selected row from all the pickers and I want to be able to have the pickers load those saved rows at start up. As of now I have found this code:

[settingsPagePicker selectRow:3 inComponent:0 animated:YES];

It works but only when the user taps the picker. I need it to work when the app is first loaded. If I put this code at viewDidLoad the app will crash.
Anyone know where the proper place to put this in my code to make it work?

Thank you for your time!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

迷爱 2024-08-16 03:38:42

在调用 -selectRow:inComponent:animated 之前,您是否检查了 settingsPagePicker 的数据源,以确保该索引处有可用数据(示例代码中的 3)?

您如何为数据源加载数据?您可以先在 viewDidLoad 中初始化数据源,然后在知道有可用数据后调用 selectRow。

更新:您的代码应该如下所示或类似的内容:

- (void)viewDidLoad
{
    [super viewDidLoad];
    pickerDataSource = [[NSMutableArray alloc] init];
    [pickerDataSource addObject:@"Item 01"];
    [pickerDataSource addObject:@"Item 02"];
    [pickerDataSource addObject:@"Item 03"];
    [pickerDataSource addObject:@"Item 04"];

    // Might want to move this to -viewWillAppear:animated
    [settingsPagePicker selectRow:3 inComponent:0 animated:YES];
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    if (pickerView == settingsPagePicker)
    {
        return [pickerDataSource objectAtIndex:row];
    }
    return @"";
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component 
{
    if (pickerView == settingsPagePicker)
    {
        return [pickerDataSource count];
    }
    return 0;
}

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)thePickerView
{
    return 1;
}

Have you checked your data source for the settingsPagePicker before calling -selectRow:inComponent:animated to make sure there is data available at that index (3 in your sample code)?

How are you loading your data for your data source? You can initialize your data source in viewDidLoad first and then call selectRow once you know there is data available.

UPDATE: Here is what your code should look like or something like it:

- (void)viewDidLoad
{
    [super viewDidLoad];
    pickerDataSource = [[NSMutableArray alloc] init];
    [pickerDataSource addObject:@"Item 01"];
    [pickerDataSource addObject:@"Item 02"];
    [pickerDataSource addObject:@"Item 03"];
    [pickerDataSource addObject:@"Item 04"];

    // Might want to move this to -viewWillAppear:animated
    [settingsPagePicker selectRow:3 inComponent:0 animated:YES];
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    if (pickerView == settingsPagePicker)
    {
        return [pickerDataSource objectAtIndex:row];
    }
    return @"";
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component 
{
    if (pickerView == settingsPagePicker)
    {
        return [pickerDataSource count];
    }
    return 0;
}

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)thePickerView
{
    return 1;
}
不忘初心 2024-08-16 03:38:42

这是我课堂上的一些方法。

- (void)viewDidLoad {
   [超级viewDidLoad];
   当前组件数量 = 1;
   //[选择器 selectRow:2 inComponent:0 动画:NO];
}
- (void) viewDidAppear:(BOOL)animated{
   [super viewDidAppear:animated];
   [picker selectRow:2 inComponent:0 animated:YES];
}

#pragma mark picker view data source methods

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return currentNumbersOfComponents;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    return 5;
}

picker.dataSource 是 self

Here some methods from my class.

- (void)viewDidLoad {
   [super viewDidLoad];
   currentNumbersOfComponents = 1;
   //[picker selectRow:2 inComponent:0 animated:NO];
}
- (void) viewDidAppear:(BOOL)animated{
   [super viewDidAppear:animated];
   [picker selectRow:2 inComponent:0 animated:YES];
}

#pragma mark picker view data source methods

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return currentNumbersOfComponents;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    return 5;
}

picker.dataSource is self

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文