简体   繁体   中英

error passing array of object between view and tableview

I'm struggling on this problem where I try to pass a NSMutableArray of objects from a view controller to UITableController but I Always get an error like this:

2013-06-08 00:08:42.098 iCollege[892:c07] -[UITableViewController setCourses:]: unrecognized selector sent to instance 0x967e4d0
2013-06-08 00:08:42.100 iCollege[892:c07] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UITableViewController setCourses:]: unrecognized selector sent to instance 0x967e4d0'

what my app doing is basically load the array from an archived file and then send it to UITableViewController through preprareForSegue method to present the data . There is 3 viewcontroller

  • ViewController (where the user enter the course information and save it or load it to array)
  • ShowViewController (this is the table view controller)
  • DaysViewController (Where the user choose the course days)

and this is the code

ViewController.h

#import <UIKit/UIKit.h>
#import "DaysViewController.h"
#import "Course.h"
#import "ShowViewController.h"

@interface ViewController : UIViewController <DaysDelegate>
{
    IBOutlet UITextField *nameTx;
    IBOutlet UITextField *hourTX;
    NSMutableArray *days;
    NSMutableArray *objects;
}

-(IBAction)save;
-(IBAction)load;
-(NSString *)getFilePath;
@end

ViewController.m

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController
#define File_Path [NSHomeDirectory()stringByAppendingPathComponent:@"Documents/courses.dat"]
- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(NSString *)getFilePath
{
    return File_Path;
}
-(IBAction)save
{
    Course *course = [Course new];
    course.name = nameTx.text;
    course.hours = [hourTX.text intValue];
    course.days = [[NSMutableArray alloc]initWithArray:days];
    if (objects == nil) {
        NSLog(@"Object array is null and its been created");
        objects = [[NSMutableArray alloc]init];
        [objects addObject:course];
    }
    else
    {
        [objects addObject:course];
    }

    NSData *data = [NSKeyedArchiver archivedDataWithRootObject:objects];
    [NSKeyedArchiver archiveRootObject:data toFile:[self getFilePath]];

}

-(IBAction)load
{
    if (objects == nil) {
        NSLog(@"Creating array before loading");
        objects = [[NSMutableArray alloc]init];
    }
    NSData *data = [NSKeyedUnarchiver unarchiveObjectWithFile:[self getFilePath]];
    objects = [NSKeyedUnarchiver unarchiveObjectWithData:data];
    for (int i=0; i<[objects count]; i++) {
        Course *c = [objects objectAtIndex:i];
        NSLog(@"Course name is %@ with credit hours %d and days %@",c.name,c.hours,c.days);
    }
}


-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"Days"]) {
        DaysViewController *dayscontroller = [segue destinationViewController];
        dayscontroller.delegate = self;

    }
    else if ([segue.identifier isEqualToString:@"show"])
    {
        if (objects == nil)
        {
            [self load];
            NSLog(@"array was nil when doing the segue now creating one");
        }

        ShowViewController *controller = [segue destinationViewController];
        [controller sendArray:objects];




    }
}


-(void)DoneSelecting:(DaysViewController *)controller withArray:(NSMutableArray *)array
{
    days = [[NSMutableArray alloc]initWithArray:array];
    [self.navigationController popToRootViewControllerAnimated:YES];
    NSLog(@"After Delegate the days are : %@",days);
}
@end

ShowViewController.h

#import <UIKit/UIKit.h>
#import "ViewController.h"
#import "Course.h"

@interface ShowViewController : UITableViewController
{
    NSMutableArray *courses;
}
@property(strong,nonatomic)NSMutableArray *courses;
-(void)sendArray:(NSMutableArray *)array;
@end

ShowViewController.m

#import "ShowViewController.h"

@interface ShowViewController ()

@end

@implementation ShowViewController
@synthesize courses;

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

//    ViewController *controller =[[ViewController alloc]init];
//    NSString *path = [controller getFilePath];
//    NSData *data = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
//    courses = [NSKeyedUnarchiver unarchiveObjectWithData:data];
//    NSLog(@"My Array for table is %@",courses);

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
#warning Potentially incomplete method implementation.
    // Return the number of sections.
    return 0;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
#warning Incomplete method implementation.
    // Return the number of rows in the section.
    return 0;
}

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

    // Configure the cell...

    return cell;
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }   
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Navigation logic may go here. Create and push another view controller.
    /*
     <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil];
     // ...
     // Pass the selected object to the new view controller.
     [self.navigationController pushViewController:detailViewController animated:YES];
     */
}


-(void)sendArray:(NSMutableArray *)array
{
    // There is problem sending the array it must be resolved.
    courses = array;
    NSLog(@"The courses array that will show to table is %@ and it has %d objects",courses,[courses count]);

}

@end

It maybe a noobish mistake but I'm new to xCode programming and I searched solution before I post bt nothing worked well for me I hope you would help me. Thank you very much

I would bet, that you set up the controller in XIB/Storyboard. You forget to set the right class name in the inspector.

在此输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM