当用户未选择任何值时如何保存默认的 UIPicker 值

发布于 2024-12-25 05:41:44 字数 1547 浏览 2 评论 0原文

我有一组选择器,每个选择器都有一个预先选择的值,例如此代码显示 150 磅是预先选择的值:

arrayPickerWeight = [[NSMutableArray alloc] initWithCapacity:100];
for ( int i = 80 ; i <= 360 ; i+=10 ) {
    [arrayPickerWeight addObject:[NSString stringWithFormat:@"%i", i]];
}   

// Calculate the screen's width.
float screenWidth = [UIScreen mainScreen].bounds.size.width;
float pickerWidth = screenWidth * 3 / 4;
// Calculate the starting x coordinate.
float xPoint = screenWidth / 2 - pickerWidth / 2;
pickerView = [[UIPickerView alloc] init];
// Set the delegate and datasource. 
[pickerView setDataSource: self];
[pickerView setDelegate:   self];
// Set the picker's frame to 50px.
[pickerView setFrame: CGRectMake(xPoint, 50.0f, pickerWidth, 180.0f)];
pickerView.showsSelectionIndicator = YES;
// Set the default value to be displayed
[pickerView selectRow:7 inComponent:0 animated:YES];
[self.view addSubview: pickerView];
[pickerView reloadAllComponents];

如果用户选择一个值,一切都会正常工作。此方法按预期调用:

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component 
{
    message = [arrayPickerWeight objectAtIndex:row];
}

但是,如果他们只是单击“保存”按钮而不移动选择器,我的消息字段中将显示 null。所以我手工编写了这段代码,将其设置为 kDefaultPickerWeight,即 150 磅。

//if picker value is null then populate with default shown    
if ([message length] == 0)
{
    message = [NSString stringWithFormat:@"%d",kDefaultPickerWeight];
}

但是,是否有我遗漏的东西或者我需要做些什么才能将 150 磅自动存储在我的消息变量中,或者我是否必须像我一样手动编码该值?

谢谢

I have a set of pickers, each of which have a pre-selected value such as this code shows where 150 lbs is the pre-selected value:

arrayPickerWeight = [[NSMutableArray alloc] initWithCapacity:100];
for ( int i = 80 ; i <= 360 ; i+=10 ) {
    [arrayPickerWeight addObject:[NSString stringWithFormat:@"%i", i]];
}   

// Calculate the screen's width.
float screenWidth = [UIScreen mainScreen].bounds.size.width;
float pickerWidth = screenWidth * 3 / 4;
// Calculate the starting x coordinate.
float xPoint = screenWidth / 2 - pickerWidth / 2;
pickerView = [[UIPickerView alloc] init];
// Set the delegate and datasource. 
[pickerView setDataSource: self];
[pickerView setDelegate:   self];
// Set the picker's frame to 50px.
[pickerView setFrame: CGRectMake(xPoint, 50.0f, pickerWidth, 180.0f)];
pickerView.showsSelectionIndicator = YES;
// Set the default value to be displayed
[pickerView selectRow:7 inComponent:0 animated:YES];
[self.view addSubview: pickerView];
[pickerView reloadAllComponents];

Everything works fine if the user picks a value. This method gets called as expected:

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component 
{
    message = [arrayPickerWeight objectAtIndex:row];
}

However, if they just click the save button without moving the picker, I get null in my message field. So I hand-coded this code to set it to kDefaultPickerWeight, which is 150 lbs.

//if picker value is null then populate with default shown    
if ([message length] == 0)
{
    message = [NSString stringWithFormat:@"%d",kDefaultPickerWeight];
}

However, is there something I'm missing or something I need to do to get the 150 lbs automatically stored in my message variable or do i have to hand-code the value like I did?

Thanks

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

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

发布评论

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

评论(2

初与友歌 2025-01-01 05:41:44

如果我是你,我会使用 NSUserDefaults。如果选择器值尚未更改,或者即使选择器第一次出现,您也可以从 NSUserDefaults 中读取。您还可以将用户的输入保存在 NSUserDefaults 中。

如果应用程序是第一次运行,您可以在应用程序委托中使用一个方法将 NSUserDefaults 设置为预定义值。

http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSUserDefaults_Class/Reference/Reference.html

If I were you, I would use NSUserDefaults. If picker value hasn't been changed, or even when the picker is appeared for the first time, you can read from NSUserDefaults. You can also save user's input in NSUserDefaults.

If the app is running for the very first time, you can have a method in your app delegate to set the NSUserDefaults to a predeifned value.

http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSUserDefaults_Class/Reference/Reference.html

只为守护你 2025-01-01 05:41:44

我认为这可能对您有帮助:

- (NSInteger)selectedRowInComponent:(NSInteger)component

您可以调用此方法来确定当前选择了哪一行,并且不必先与选择器进行任何用户交互。因此,如果您想在第一次启动视图时找到所选行,那么您将使用它。

例子:

int row = [picker selectedRowInComponent:0];
NSLog(@"Selected row = %i", row);

I think this might help you:

- (NSInteger)selectedRowInComponent:(NSInteger)component

You can call this method to figure out which row is currently selected and there does not have to be any user interaction with the picker first. So if you want to find out the selected row when you first launch the view that's what you would use.

Example:

int row = [picker selectedRowInComponent:0];
NSLog(@"Selected row = %i", row);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文