未知类型名称' studentController'

时间:2014-08-16 23:45:37

标签: objective-c uitableview

我正在创建一个表视图应用程序,它不会识别studentAddController.h中的studentController类型。我有一个以前看过的项目,无法弄清楚为什么它无法识别类型名称。

这是studentController.h中的代码

 #import <UIKit/UIKit.h>
 #import "studentCells.h"
 #import "AssignmentsController.h"
 #import "studentAddController.h"


 @interface studentController : UITableViewController {
    NSMutableArray *studentArray;
 }
 - (IBAction)addStudentButton:(id)sender;
 - (void)insertNewRow:(NSDictionary *)studentDictionary;
 @property (nonatomic, retain) UITableView *studentTableView;

 @end

以下是studentController.m

中的代码
#import "studentController.h"

@implementation studentController

-(id)initWithStyle:(UITableViewStyle)style {
self =[super initWithStyle:style];
if (self) {

}
return self;
}

-(void)viewDidLoad {
[super viewDidLoad];
 studentArray = [[NSMutableArray alloc] initWithObjects: nil];
}

- (IBAction)addStudentButton:(id)sender {
     studentAddController *studentAddView =[self.storyboard       instantiateViewControllerWithIdentifier:@"studentAddView"];
[self.navigationController pushViewController:studentAddView animated:YES];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}

 -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [studentArray count];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *studentCellIdentifier = @"studentCell";
studentCells *cell = [tableView dequeueReusableCellWithIdentifier:studentCellIdentifier forIndexPath:indexPath];
NSDictionary *studentDictionary = [studentArray objectAtIndex:[indexPath row]];
[cell.firstNameCellLabel setText:[studentDictionary objectForKey:@"First Name"]];
[cell.lastNameCellLabel setText:[studentDictionary objectForKey:@"Last Name"]];
return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
AssignmentsController *assignmentView = [self.storyboard instantiateViewControllerWithIdentifier:@"assignmentView"];
[self.navigationController pushViewController:assignmentView animated:YES];
}

-(void)insertNewRow:(NSDictionary *)studentDictionary {
[studentArray addObject:studentDictionary];
[self.tableView beginUpdates];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:[studentArray count] -1 inSection:0];
[self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
[self.tableView endUpdates];

}

@end

以下是studentAddController.h的代码

#import <UIKit/UIKit.h>
#import "studentController.h"
#import "studentAddController.h"

@interface studentAddController : UIViewController
@property (weak, nonatomic) IBOutlet UITextField *firstNameField;
@property (weak, nonatomic) IBOutlet UITextField *lastNameField;

- (IBAction)doneButton:(id)sender;
- (IBAction)keyboardResign:(id)sender;

@property (nonatomic, strong) studentController *parentTableVC;

@end

以下是studentAddController.m

中的代码
#import "studentAddController.h"

@implementation studentAddController

- (IBAction)doneButton:(id)sender {
NSDictionary *studentDictionary =@{ @"First Name" : [self.firstNameField text],
                                    @"Last Name" : [self.lastNameField text]
                                    };
[self.parentTableVC insertNewRow:studentDictionary];
[self.navigationController popViewControllerAnimated:YES];
}

- (IBAction)keyboardResign:(id)sender {

[self resignFirstResponder];
}
@end

1 个答案:

答案 0 :(得分:1)

您有循环参考。在studentController.h导入studentAddController.h,在studentAddController.h导入studentController.h。尝试对标题进行这些更改:

studentController.h

#import <UIKit/UIKit.h>
#import "studentCells.h"
#import "AssignmentsController.h"

studentController.m

#import "studentController.h"
#import "studentAddController.h"

studentAddController.h

#import <UIKit/UIKit.h>
#import "studentController.h"

我还要注意,命名标准会指示你的类是PascalCase而不是camelCase。只有你的变量和方法应该是camelCase。

我还要注意,你不需要在studentController.h中导入studentAddController,所以我把它移到.m。如果您确实需要导入studentAddController,那么您将执行前向类声明以避免循环引用。像这样:

#import <UIKit/UIKit.h>
#import "studentCells.h"
#import "AssignmentsController.h"

@class studentAddController;