每当按下 Save 按钮时,我都会使用委托将对象从我的 AssignmentViewController 传递给我的 TableViewController。
我正在尝试打印出我应该刚刚保存并传递的对象的内容,但 NSLog 显示(空)。有人可以帮我弄清楚为什么吗?
分配视图控制器.h
@protocol AssignmentDelegate <NSObject>
-(void)newAssignment:(Homework *)assignment;
@end
@interface Assignment : UIViewController <UITextViewDelegate>
@property (nonatomic,weak)id<AssignmentDelegate> delegate;
@property(nonatomic) IBOutlet UITextField *ClassNameField;
@property(nonatomic) IBOutlet UILabel *ClassNameLabel;
@property(nonatomic) IBOutlet UITextField *AssignmentTitleField;
@property(nonatomic) IBOutlet UILabel *AssignmentTitleLabel;
@property (nonatomic, strong) Homework *homeworkAssignment;
- (IBAction)Save:(UIButton *)sender;
分配视图控制器.m
- (IBAction)Save:(UIButton *)sender {
self.homeworkAssignment = [[Homework alloc] init];
self.homeworkAssignment.className = self.ClassNameField.text;
self.homeworkAssignment.assignmentTitle = self.AssignmentTitleField.text;
self.homeworkAssignment.assignmentDiscription = self.DiscriptionTextView.text;
[self.delegate newAssignment:self.homeworkAssignment];
NSLog(@"%@",self.delegate); //PRINTS (null)*******************************
[self.navigationController popViewControllerAnimated:YES];
}
这是我的 TableViewController.h 以防问题出在此处
@interface AssignmentTableController : UITableViewController
<
AssignmentDelegate
>
@property (strong, nonatomic) NSMutableArray *alist;
@end
表视图控制器.m
-(NSMutableArray *)alist
{
if (!_alist) {
_alist = [[NSMutableArray alloc]init];
}
return _alist;
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if([segue.identifier isEqualToString:@"to2"]) {
Assignment *assignmentViewController = segue.destinationViewController;
assignmentViewController.delegate = self;
}
}
-(void)newAssignment:(Homework *)assignment
{
[self.alist addObject:assignment];
NSString *filePath = [self dataFilePath];
[NSKeyedArchiver archiveRootObject:self.alist toFile:filePath];
[self.tableView reloadData];
}