简体   繁体   English

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

[英]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: 我正在努力解决这个问题,我试图将一个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'

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

  • ViewController (where the user enter the course information and save it or load it to array) ViewController(用户输入课程信息并保存或加载到数组中)
  • ShowViewController (this is the table view controller) ShowViewController(这是表视图控制器)
  • DaysViewController (Where the user choose the course days) DaysViewController(用户选择课程天数的地方)

and this is the code 这是代码

ViewController.h 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 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 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 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. 这可能是一个noobish错误,但我是xCode编程的新手,我在发布bt之前搜索了解决方案,对我来说没什么用,我希望你能帮助我。 Thank you very much 非常感谢你

I would bet, that you set up the controller in XIB/Storyboard. 我敢打赌,你在XIB / Storyboard中设置了控制器。 You forget to set the right class name in the inspector. 您忘记在检查器中设置正确的类名。

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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