-2

I am using delegation to pass an object from my AssignmentViewController to my TableViewController whenever my Save button is pressed.

I am trying to print out the content of the object that I should have just saved and passed over, but NSLog is showing (null). Can someone help me figure out why?

AssignmentViewController.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;

AssignmentViewController.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];
}

And here Is my TableViewController.h incase the problem lies in here

@interface AssignmentTableController : UITableViewController
<
AssignmentDelegate
>

@property (strong, nonatomic) NSMutableArray *alist;


@end

TableViewController.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];
}
Mike
  • 477
  • 2
  • 7
  • 24
  • Have you made sure that the segue.identifier is *to2*. In short have you made sure that the code in - prepareForSegue runs? Try putting a break point where the delegate is assigned. – Hugo Tunius Oct 22 '13 at 19:41
  • The object you're printing does not exist anymore. It has been deallocated and the weak reference set to `nil`. Figure out what other object is supposed to be owning it and why it stopped owning it. – jscs Oct 22 '13 at 19:41
  • Where do you call `save`? – Firo Oct 22 '13 at 19:42
  • This question appears to be off-topic because it is a request for an interactive debugging session. – jscs Oct 22 '13 at 19:42
  • @HugoT I set the Segue identifier to "to2" in storyboard. So I assume that it is running. – Mike Oct 22 '13 at 19:44
  • @Firo Save is called when you press the save button on my AssignmentViewController – Mike Oct 22 '13 at 19:45
  • @MikeGordon That might be the case, but still make sure that the prepareForSegue code is running. This is the most likely place of failure – Hugo Tunius Oct 22 '13 at 19:48
  • @HugoT I checked storyboard to make sure that the Identifier was set to "to2". But it appeared blank, so I must have done something at some point that got rid of it. If you put your suggestion in an answer, ill chose it. Its working now – Mike Oct 22 '13 at 19:51

1 Answers1

0

Make sure that - (void) prepareForSegue is running by verifying the segue id and putting a beakpoint inside the if statement in the method.

Hugo Tunius
  • 2,869
  • 24
  • 32