-1

I have a Assignment ViewController and a TableViewController.

The assignment View Controller takes input and saves the information in an object.

What I need is , using delegation, alert the tableviewcontroller that an assignment was created, and have the tableviewcontroller add the object to a NSMutableArray, and archive it.

It seems easy but I am having a hard time understanding delegation.

Here is the save Method - 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.homeworkAssignment.pickerDate = self.DatePicker.date;

 NSMutableArray *MyHomeworkArray = [[NSMutableArray alloc] init];
[MyHomeworkArray addObject:self.homeworkAssignment];


 NSString *filePath = [self dataFilePath];
//Archive my object
[NSKeyedArchiver archiveRootObject:MyHomeworkArray toFile:filePath];
}

My save method currently saves the info, adds to an array, and archives. But I need to use delegation between my TableViewController and my AssignmentViewController, and have my tableViewCOntroller alerted when save is pressed, and then add to the array and archive it itself.

Can someone please help me set this up correctly using delegation?

Mike
  • 477
  • 2
  • 7
  • 24
  • Do you have some snapshot ?? What exactly do you want to do ?? Do you want to create a delegate for your tableviewcontroller or assignmentviewcontroller ? – Kunal Balani Oct 13 '13 at 22:04
  • @KunalBalani I need to alert the TableView whenever the save button is pressed from the other controller. The tableView will then add the object From the other controller to an array and archive the array to a file. Im not sure which one is supposed to be the delegate or how to create it. – Mike Oct 13 '13 at 22:08
  • You don't need delegation here .. You can just use [tableView reload] and tableViewController will refresh data for you – Kunal Balani Oct 13 '13 at 22:09
  • @KunalBalani I think the TableView needs to be a delegate of The assignmentView Controller. Data is being passed from assignmentViewController to the Table View – Mike Oct 13 '13 at 22:10
  • Are you using UINavigationBAr or UITabBarController for transition? – Kunal Balani Oct 13 '13 at 22:10
  • @KunalBalani UINavigationBar. – Mike Oct 13 '13 at 22:10
  • That's tableview's datasource , delegate is for receiving callbacks on selecting a cell. – Kunal Balani Oct 13 '13 at 22:11
  • @KunalBalani Well the assignmentViewController is supposed to send am message to TableView when the save button is pressed. – Mike Oct 13 '13 at 22:15

1 Answers1

0

Add property for your array in UITableViewController:

@property (nonatomic,retain) NSMutableArray *homeworkArray;

now on save method:


-(IBAction)Save:(UIButton *)sender {

   UITableViewController *tabVC = [[UITableViewController alloc] init];
   tableVC.homeworkArray = self. MyHomeworkArray; // send a message to tableview
   [self.navigationcontroller pushViewController:tableVC];
}

Now in TableViewController you need to set override your datasource method to show text on cell

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.homeworkArray count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    // Configure the cell...
    if(!cell){
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    //update your cell here
    cell.textLabel.text =  self.homeworkAssignment.className 
    cell.detailTextLabel.text = self.AssignmentTitleField.text;
    return cell;
}

Kunal Balani
  • 4,739
  • 4
  • 36
  • 73
  • tableVC.homeworkArray = self. MyHomeworkArray; <-- This line in the save method is what transfers the data between controllers, am I correct about that? – Mike Oct 13 '13 at 22:19
  • Yes. You need to pass data when you are loading your view controller. However if the tableViewcontroller is already loaded then you just need to call reloadData method. – Kunal Balani Oct 13 '13 at 22:21
  • And in which method in TableView should I do the archiving of the Array to a file when the save button is pushed? – Mike Oct 13 '13 at 22:23
  • Archiving should not be done in tableviewcontroller (it is just used to display) you should archive in your save method. – Kunal Balani Oct 13 '13 at 22:24
  • I specifically asked our instructor that question, and she said that the tableViewController should be doing the archiving/unarchiving, and that it should be responsible for persisting changes in the data. At the moment, all I need is for the TableView to archive the data passed to it, no more.I have the code to achive, i just don't know where to place it in TableView – Mike Oct 13 '13 at 22:28
  • That's a bad practice. Persistance storage should be done in a different class (like data controller). Not for you inform your prof. But it you want to do it there is a method called as viewDidLoad or viewWillAppear. Do it any of them doesn't make a difference – Kunal Balani Oct 13 '13 at 22:35