[英]error passing array of object between view and tableview
我正在努力解決這個問題,我試圖將一個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.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
這可能是一個noobish錯誤,但我是xCode編程的新手,我在發布bt之前搜索了解決方案,對我來說沒什么用,我希望你能幫助我。 非常感謝你
我敢打賭,你在XIB / Storyboard中設置了控制器。 您忘記在檢查器中設置正確的類名。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.