在 Objective-C 中传回 NSManagedObject
Passing NSManagedObject back in Objective-C
我已经尝试按照此 post 回复:将数据传回之前的 viewController,但运气不佳。我花在这上面的时间比我愿意承认的要多,我已经阅读了所有可能的内容,但我在这里找到的任何内容都不起作用。
Passing Data between View Controllers
这是我正在尝试做的事情:
Legend: AddPOIViewController -----------> CategoryViewController
Step 1: empty textField ---segue---> selectCategory
Step 2: setCategory on CategoryViewController
Step 3: AddPOIViewController <--delegate-- setCategory
我有一个 AddPOIViewController
(A) 和一个 CategoryViewController
(B)。我有一个 segue
从 A 到 B 都能正常工作。我想 select B 中的一个类别并将其转储回 A。
这是我所做的:
AddPOIViewController.h
在我从 AddPOIViewController
到 CategoryTableViewController
的 segue 中,我将委托设置如下:
CategoryTableViewController *categoryTableViewController = [navigationController viewControllers][0];
categoryTableViewController.delegate = (id<CategoryTableViewControllerDelegate>)self;
// Delegate method
- (void)addItemViewController:(CategoryTableViewController *)controller didSelectCategory:(LocationCategory *)selectedCategory {
NSLog(@"This was returned from CategoryTVC: %@", self.selectedCategory);
}
CategoryTableViewController.h
// This property sets CategoryTVC as the delgate of AddPOIViewController
@property (nonatomic, weak) id <CategoryTableViewControllerDelegate> delegate;
// This property is public so the AddPOIVC knows about it
@property (nonatomic, strong) LocationCategory *selectedCategory;
CategoryTableViewController.h
- (IBAction)done:(id)sender {
[self.delegate categoryTableViewControllerDidSave:self];
// This passes the value back to the AddPOIViewController
[self.delegate addItemViewController:self didSelectCategory:self.selectedCategory];
}
我认为问题出在 CategoryTableViewController
中的 IBAction done:
方法中:
- (IBAction)done:(id)sender {
[self.delegate categoryTableViewControllerDidSave:self];
// This passes the value back to the AddPOIViewController
[self.delegate addItemViewController:self didSelectCategory:self.selectedCategory];
}
问题在于此方法的第一行调用了委托的 categoryTableViewControllerDidSave:
方法,该方法关闭了视图控制器:
[self dismissViewControllerAnimated:YES completion:nil];
因此 CategoryTableViewController
在到达下一行之前被解散,这是传递 NSManagedObject 的地方。
要修复它,只需在 IBAction done:
方法中切换两行:
- (IBAction)done:(id)sender {
// This passes the value back to the AddPOIViewController
[self.delegate addItemViewController:self didSelectCategory:self.selectedCategory];
// This causes this view controller to be dismissed:
[self.delegate categoryTableViewControllerDidSave:self];
}
您还需要保存调用中传递给 AddPOIViewController
的 selectedCategory
属性 的 selectedCategory
:
- (void)addItemViewController:(CategoryTableViewController *)controller didSelectCategory:(LocationCategory *)selectedCategory {
self.selectedCategory = selectedCategory;
NSLog(@"This was returned from CategoryTVC: %@", self.selectedCategory);
}
我已经尝试按照此 post 回复:将数据传回之前的 viewController,但运气不佳。我花在这上面的时间比我愿意承认的要多,我已经阅读了所有可能的内容,但我在这里找到的任何内容都不起作用。
Passing Data between View Controllers
这是我正在尝试做的事情:
Legend: AddPOIViewController -----------> CategoryViewController
Step 1: empty textField ---segue---> selectCategory
Step 2: setCategory on CategoryViewController
Step 3: AddPOIViewController <--delegate-- setCategory
我有一个 AddPOIViewController
(A) 和一个 CategoryViewController
(B)。我有一个 segue
从 A 到 B 都能正常工作。我想 select B 中的一个类别并将其转储回 A。
这是我所做的:
AddPOIViewController.h
在我从 AddPOIViewController
到 CategoryTableViewController
的 segue 中,我将委托设置如下:
CategoryTableViewController *categoryTableViewController = [navigationController viewControllers][0];
categoryTableViewController.delegate = (id<CategoryTableViewControllerDelegate>)self;
// Delegate method
- (void)addItemViewController:(CategoryTableViewController *)controller didSelectCategory:(LocationCategory *)selectedCategory {
NSLog(@"This was returned from CategoryTVC: %@", self.selectedCategory);
}
CategoryTableViewController.h
// This property sets CategoryTVC as the delgate of AddPOIViewController
@property (nonatomic, weak) id <CategoryTableViewControllerDelegate> delegate;
// This property is public so the AddPOIVC knows about it
@property (nonatomic, strong) LocationCategory *selectedCategory;
CategoryTableViewController.h
- (IBAction)done:(id)sender {
[self.delegate categoryTableViewControllerDidSave:self];
// This passes the value back to the AddPOIViewController
[self.delegate addItemViewController:self didSelectCategory:self.selectedCategory];
}
我认为问题出在 CategoryTableViewController
中的 IBAction done:
方法中:
- (IBAction)done:(id)sender {
[self.delegate categoryTableViewControllerDidSave:self];
// This passes the value back to the AddPOIViewController
[self.delegate addItemViewController:self didSelectCategory:self.selectedCategory];
}
问题在于此方法的第一行调用了委托的 categoryTableViewControllerDidSave:
方法,该方法关闭了视图控制器:
[self dismissViewControllerAnimated:YES completion:nil];
因此 CategoryTableViewController
在到达下一行之前被解散,这是传递 NSManagedObject 的地方。
要修复它,只需在 IBAction done:
方法中切换两行:
- (IBAction)done:(id)sender {
// This passes the value back to the AddPOIViewController
[self.delegate addItemViewController:self didSelectCategory:self.selectedCategory];
// This causes this view controller to be dismissed:
[self.delegate categoryTableViewControllerDidSave:self];
}
您还需要保存调用中传递给 AddPOIViewController
的 selectedCategory
属性 的 selectedCategory
:
- (void)addItemViewController:(CategoryTableViewController *)controller didSelectCategory:(LocationCategory *)selectedCategory {
self.selectedCategory = selectedCategory;
NSLog(@"This was returned from CategoryTVC: %@", self.selectedCategory);
}