Objective c 根据coredata中匹配的字符串选择UIPickerview
我有一个表单,一旦用户提交,它应该重新加载视图(但会改变一些事情) 在此表单上有一个UIPickerview(_tripPicker),它有两个位置:起始位置和结束位置(pickerview的两个组件) 我将它保存到适当的数据库和所有这些——但是当它重新加载(当用户单击保存时),我希望pickerview“重置”,第一个位置(begSchool)与用户刚刚保存到coredata的第二个位置(endSchool)相匹配 例如:假设用户从点B转到点C(pickerview中的组件分别为0和1);当他们单击“保存”时,我希望组件0显示“PointC”,而第二个组件只显示要转到的点列表(将其重置为最初的加载方式) 我尝试过做一些匹配字符串的工作,但遇到了一些问题 以下是我这样做的逻辑:Objective c 根据coredata中匹配的字符串选择UIPickerview,objective-c,core-data,uipickerview,Objective C,Core Data,Uipickerview,我有一个表单,一旦用户提交,它应该重新加载视图(但会改变一些事情) 在此表单上有一个UIPickerview(_tripPicker),它有两个位置:起始位置和结束位置(pickerview的两个组件) 我将它保存到适当的数据库和所有这些——但是当它重新加载(当用户单击保存时),我希望pickerview“重置”,第一个位置(begSchool)与用户刚刚保存到coredata的第二个位置(endSchool)相匹配 例如:假设用户从点B转到点C(pickerview中的组件分别为0和1);当他
//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中被选中。文本当前显示适当的名称,但pickerview未显示任何更改
请告诉我是否需要澄清,或者您需要查看哪些其他代码-提前感谢。我可以通过将字符串与数组中的对象进行匹配,首先在数组中找到indexValue来解决此问题 我的结局是:
//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];