objective-c - 根据匹配来自 coredata 的字符串选择 UIPickerview

标签 objective-c core-data uipickerview

我有一个表单,一旦用户提交,它应该重新加载 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/

相关文章:

iphone - 创建数据库后无法执行FetchRequest

ios - 使用 UITextField + UIPickerview 时禁用键盘输入

ios - UIPickerView 从 UISegmentedControl 更新

ios - 画线 objective-c

iphone - 如何使用 withVoice : argument in FliteController class? 放慢声音

ios - 检测 View 已成功更新框架

arrays - 如何设置选择器 View 以显示来自多个矩阵选项的数据?

iphone - fbDidLogin 的问题从未调用过 iOS

iphone - 如何使用 Core Data 维护 UITableView 中的显示顺序?

ios - Swift - 使用 CoreData 从数据库预加载和更新数据