当用户未选择任何值时如何保存默认的 UIPicker 值
我有一组选择器,每个选择器都有一个预先选择的值,例如此代码显示 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果我是你,我会使用 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
我认为这可能对您有帮助:
您可以调用此方法来确定当前选择了哪一行,并且不必先与选择器进行任何用户交互。因此,如果您想在第一次启动视图时找到所选行,那么您将使用它。
例子:
I think this might help you:
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: