错误在视图和tableview之间传递对象数组

时间:2013-06-07 21:21:08

标签: ios nsmutablearray uitabbarcontroller

我正在努力解决这个问题,我尝试将一个NSMutableArray对象从视图控制器传递给UITableController,但我总是得到这样的错误:

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'

我的应用程序所做的基本上是从存档文件加载数组,然后通过preprareForSegue方法将其发送到UITableViewController来呈现数据。有3个viewcontroller

  • ViewController(用户输入课程信息并保存或加载到阵列中)
  • ShowViewController(这是表视图控制器)
  • DaysViewController(用户选择课程日期)

这是代码

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

这可能是一个愚蠢的错误,但我是xCode编程的新手,我在发布bt之前搜索了解决方案,对我来说没什么用,我希望你能帮助我。非常感谢你

1 个答案:

答案 0 :(得分:2)

我敢打赌,你在XIB / Storyboard中设置了控制器。您忘记在检查器中设置正确的类名。

enter image description here