我有一組選擇器,其中每個都有一個預先選定的值,例如此代碼顯示其中150磅是預選值:當用戶不選擇任何值時如何保存默認UIPicker值
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];
}
但是,如果他們只是點擊保存按鈕而不移動選擇器,我得到了我的信息欄爲空。所以我手動編碼這個代碼,將它設置爲kDefaultPickerWeight,這是150磅。
//if picker value is null then populate with default shown
if ([message length] == 0)
{
message = [NSString stringWithFormat:@"%d",kDefaultPickerWeight];
}
然而,就是有什麼我缺少什麼,我需要做的就是150磅自動保存在我的消息變量還是我必須手工編寫的價值像我一樣?
謝謝
試圖'如果(消息==無){...}'? – pmk 2012-01-04 14:00:41
我認爲這是檢查郵件長度的有效工具。我只是想知道是否有一種方法可以將預先選定的值存儲到接收變量中,而不必檢查它是否爲空,就UIPicker的自然行爲或方法而言。謝謝 – Jazzmine 2012-01-04 14:02:34