根据与coredata匹配的字符串选择UIPickerview

时间:2014-02-20 21:21:08

标签: objective-c core-data uipickerview

我有一个表单,一旦用户提交,它应该重新加载视图(但改变一些东西)。

在此表单上有一个UIPickerview(_tripPicker),它有2个位置,一个起始位置和一个结束位置(两个组件到选择器视图)。

我将它保存到适当的数据库和所有这些 - 但是当它重新加载时(当用户点击保存时)我希望pickerview'重置',并且第一个位置(begSchool),以匹配用户的第二个位置( endSchool)他们刚刚保存到coredata。

例如:假设用户从PointB转到pointC(pickerview中分别为0和1);当他们点击保存时,我希望组件0显示“PointC”,而第二个组件只显示要去的点列表(将其重置为最初加载的方式)。

我试图尝试做一些匹配的字符串,但我遇到了问题。

以下是我执行此操作的逻辑:

//Save the data & reload View
[[NSManagedObjectContext MR_defaultContext] MR_saveToPersistentStoreWithCompletion:^(BOOL success, NSError *error) {

//Reset the pickerview  **********THIS LOGIC NEEDS WORK**********
//Check to see if there is previous UserMiles entered - if so, set beg_school to appropriate name
// Get the local context
NSArray *tripsSorted = [UserMiles MR_findAllSortedBy:@"driven_date" ascending:NO];
if (!tripsSorted || !tripsSorted.count){
    //if no previous trips have been entered - set the school list to default
    begSchoolLabel.text = [_schoolArray1 objectAtIndex:[_tripPicker selectedRowInComponent:0]];

} else {
    UserMiles *lastTrip = [tripsSorted objectAtIndex:0];
    NSString *preValue = lastTrip.end_school;
    begSchoolLabel.text = preValue;
    NSUInteger *currentIndex = [_schoolArray1 indexOfObject:preValue]; //Error/warning that incompatible integer to point conversion
    [_tripPicker selectRow:currentIndex inComponent:0 animated:YES];  //Error/warning here that incompatible point to integer conversion
    NSLog(@"LastTrip.endSchool = %@", lastTrip.end_school);
}

//Set end school labels for the second component in the picker
endSchoolLabel.text = [_schoolArray2 objectAtIndex:[_tripPicker selectedRowInComponent:1]];

NSLog (@"saveInBackground: finished!");

}];
[self.view setNeedsDisplay];

lastTrip.end_school从pickerview的组件1中获取学校的相应名称,我只需要弄清楚如何将其与正在加载pickerview的数组中的相应值匹配,并在pickerview中选中它。文本当前显示相应的名称,但选择器视图未显示任何更改。

如果我需要澄清或需要查看其他代码,请告知我们 - 提前感谢。

1 个答案:

答案 0 :(得分:1)

我能够通过首先在数组中找到indexValue来解决这个问题,方法是将字符串与数组中的Objects匹配。

我最终得到了这个:

//Save the data & reload View
[[NSManagedObjectContext MR_defaultContext] MR_saveToPersistentStoreWithCompletion:^(BOOL success, NSError *error) {

//Reset the pickerview  **********THIS LOGIC NEEDS WORK**********
//Check to see if there is previous UserMiles entered - if so, set beg_school to appropriate name
// Get the local context
NSArray *tripsSorted = [UserMiles MR_findAllSortedBy:@"driven_date" ascending:NO];
if (!tripsSorted || !tripsSorted.count){
    //if no previous trips have been entered - set the school list to default
    begSchoolLabel.text = [_schoolArray1 objectAtIndex:[_tripPicker selectedRowInComponent:0]];

} else {
    UserMiles *lastTrip = [tripsSorted lastObject];
    NSString *preValue = lastTrip.end_school;
    begSchoolLabel.text = preValue;
    int indexValue = [_schoolArray1 indexOfObject:preValue]; //Compares the preValue string to the strings in the array and finds the indexValue of the right match
    [_tripPicker selectRow:indexValue inComponent:0 animated:YES]; //Sets the picker to the appropriate value
    NSLog(@"LastTrip.endSchool = %@", lastTrip.end_school);
}

//Set end school labels for the second component in the picker
endSchoolLabel.text = [_schoolArray2 objectAtIndex:[_tripPicker selectedRowInComponent:1]];

NSLog (@"saveInBackground: finished!");

}];
[self.view setNeedsDisplay];