2014-06-27 66 views
0

我正在使用UIPickerView並希望該部分與上次使用時相同。我看了一下蘋果的文檔,但沒有看到有關設置章節的任何內容。無法弄清楚如何設置UIPickerView的起始值

我的代碼

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view. 
    // Initialize Data 
    _pickerData = @[@"I", @"2", @"3", @"4", @"5", @"6", @"7", @"8", @"9"]; 

    // Connect data 
    picker.dataSource = self; 
    picker.delegate = self; 

} 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

/* 
#pragma mark - Navigation 

// In a storyboard-based application, you will often want to do a little preparation before navigation 
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
{ 
    // Get the new view controller using [segue destinationViewController]. 
    // Pass the selected object to the new view controller. 
} 
*/ 
- (IBAction)aOk:(id)sender { 
    [self.navigationController popViewControllerAnimated:YES]; 

    // store data 
    int i=0; 
    i=[picker selectedRowInComponent:1]*10; 
    i+=[picker selectedRowInComponent:1]; 
} 
- (IBAction)aCancel:(id)sender { 
    [self.navigationController popViewControllerAnimated:YES]; 
} 




// The number of columns of data 
- (int)numberOfComponentsInPickerView:(UIPickerView *)pickerView 
{ 
    return 2; 
} 

// The number of rows of data 
- (int)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component 
{ 
    // compment contains the col number 
    return _pickerData.count; 
} 

// The data to return for the row and component (column) that's being passed in 
- (NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component 
{ 
    return _pickerData[row]; 
} 

// Catpure the picker view selection 
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component 
{ 
    // This method is triggered whenever the user makes a change to the picker selection. 
    // The parameter named row and component represents what was selected. 
} 


@end 

回答

0

是的,我在開發我的知識問答遊戲有同樣的問題。以下是我解決問題的方法。

已加載數據後,使用此選擇一行:

UIPickerView * pickerView = [[UIPickerView alloc] initWithFrame:rect]; 
[pickerView setDelegate:self]; 
[pickerView setDataSource:self]; 
[view addSubview:pickerView]; 

//We're going to select a row with this line 
[pickerView selectRow:[_pickerData objectAtIndex:0] inComponent:0 animated:YES]; 

我看到你說你希望pickerView默認爲選擇的最後一行(我認爲這是你在做什麼試圖說)?

如果是這樣的話,我只會將最後選中的行存儲在NSUserDefaults中(即使用戶殺死應用程序也會保留這一行)。

每當用戶導航到選擇器視圖,你可以做這樣的事情:

NSInteger lastSelectedRow = [[NSUserDefaults standardUserDefaults] integerForKey:@"lastSelectedRow"]; 
if (lastSelectedRow) 
    [pickerView selectRow:lastSelectedRow inComponent:0 animated:YES]; 
else 
    [pickerView selectRow:[_pickerData objectAtIndex:0] inComponent:0 animated:YES]; 
+0

我無法得到它的工作,只是停留在把一個破發點上的if語句開始現在的位置代碼 –

+0

。 ..即如果(lastSelectedRow)。 lastSelectedRow變量是否有你正在尋找的索引?另外,你是否存儲最後選擇的索引? –