我搜索过StackOverflow和google,但仍然卡住了。 任何帮助或代码片段都表示赞赏!
所以,我看到的问题是我的委托仅在从自定义视图控制器类的第一个实例(AddJobViewController
基于UIKit.h
)调用时才响应。从第一个视图调用时它工作正常,但是当第二个视图调用时,委托返回null
。无论我哪个视图都能正确分配代理。
我在故事板中有两个分配了这个自定义类的视图。
以下是授权代码:
ATableViewController.h
:
#import "AddJobViewController.h"
@interface ATableViewController : UITableViewController <AddJobViewControllerDelegate, NSFetchedResultsControllerDelegate>
ATableViewController.m:
- (void)AddJobViewControllerSave {
NSError *error = nil;
NSManagedObjectContext *context = self.managedObjectContext;
if (![context save:&error]) {
NSLog(@"Error! %@", error);
}
}
ATableViewController.m
:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([[segue identifier] isEqualToString:@"addJob"]) {
AddJobViewController *ajvc = (AddJobViewController *)[segue destinationViewController];
ajvc.delegate=self;
AJobDetails *newJob = (AJobDetails *) [NSEntityDescription insertNewObjectForEntityForName:@"AJobDetails" inManagedObjectContext:[self managedObjectContext]];
ajvc.currentJob = newJob;
}
}
AddJobViewController.h
:
#import "AJobDetails.h"
@protocol AddJobViewControllerDelegate;
@interface AddJobViewController : UIViewController
- (IBAction)save:(id)sender;
- (IBAction)next:(id)sender;
@end
@property (weak, nonatomic) id <AddJobViewControllerDelegate> delegate;
@property (nonatomic, strong) AJobDetails *currentJob;
@protocol AddJobViewControllerDelegate
-(void)AddJobViewControllerSave;
-(void)AddJobViewControllerNext;
-(void)AddJobViewControllerDidCancel:(AJobDetails *)jobToDelete;
@end
AddJobViewController.m
:
- (IBAction)save:(id)sender {
[self.currentJob setThresholdTrimText:thresholdText.text];
NSLog(@"PVC Delegate: %@",self.delegate);
[self.delegate AddJobViewControllerSave];
}
Core Data上下文正在传递给两个视图并正常工作。