I have 2 Controllers, a AssignmentViewController and a UITableViewController. Whenever an assignment is created and the saved button is clicked, I need to send that Assignment object to my UITableViewController (Using delegation). The TableView then needs to archive it in a file in a NSMutableArray (TableView must do the archiving/unarchiving for this app).

And whenever the TableView is loaded, it needs to unarchive the array and have the Homework Objects displayed in the table cells.

I have the code started, but I don't really know what Im doing correctly and incorrectly.

The app is supposed to work in this order. Enter assignment info and press save -> send new assignment object to TableViewController using delegation -> Archive object in NSMutableArray to a file ->Whenever Table view is on the screen, unarchive array and display assignment objects in cells.

Here is what I have:

AssignmentViewController.h

@interface Assignment : UIViewController

<
UITextViewDelegate,
AssignmentTableControllerDelegate 
//Error:Cannot find protocol declaration "AssignmentTableControllerDelegate"

>
@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;
@end

AssignmentViewController.m

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


self.homeworkAssignment = [[Homework alloc] init];   
self.homeworkAssignment.className = self.ClassNameField.text;
self.homeworkAssignment.assignmentTitle = self.AssignmentTitleField.text;

//Sending info over to TableController

self.assignmentTableController.delegate= self;
self.assignmentTableController.homeworkInTable.className= self.ClassNameField.text;
 self.assignmentTableController.homeworkInTable.assignmentTitle=self.AssignmentTitleField.text;

AssignmentTABLEController.h

#import "Assignment.h"

@protocol AssignmentTableControllerDelegate;


@interface AssignmentTableController : UITableViewController

@property (nonatomic, strong) Homework *homeworkInTable;
@property (nonatomic, assign)  id <AssignmentTableControllerDelegate> delegate;
@property (nonatomic,strong) NSMutableArray *MyHomeworkArray;

-(IBAction)ArchiveData:(id)sender;

@end

@protocol AssignmentTableControllerDelegate <NSObject>

-(NSMutableArray *)UnarchiveData;
@end

AssignmentTABLECONTROLLER.M (All I did was add these 2 methods)

-(void)ArchiveData:(id)sender
{
[self.MyHomeworkArray addObject:self.homeworkInTable];


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


-(NSMutableArray *)UnarchiveData
{
Homework *archivedHomework = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
                                //Error:use of undeclared identifier:filePath^^

}

I am new to this so I know theres a lot wrong. I "think" I am passing the info over correctly from AssignmentViewController with delegation. But I am pretty sure I am not archiving/unarchiving correctly at all. I am not sure how to separate the archiving/unarchiving code and then display in the table.

有帮助吗?

解决方案

Couple of ideas to help.

1) Put the protocol in its own file, and import it as needed into the relevant .h or .m files so all your warnings go away.

2) When you have a new assignment, send it to the table view controller.

3) The table view controller should: a) insert it or append it into a mutable array. This array is the data used by the table itself. b) you can then tell the tableView to "insertRows..." to show it, or just use "reloadData". c) with that done, now you can archive the data.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top