我有一个表单,一旦用户提交,它应该重新加载 View (但要改变一些东西)。
在这个表单上有一个 UIPickerview (_tripPicker),它有 2 个位置,一个起始位置和一个结束位置(pickerview 的 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 中被选中.文本当前显示适当的名称,但 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];
关于objective-c - 根据匹配来自 coredata 的字符串选择 UIPickerview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21919867/