0

我正在尝试通过将我已成功完成的 Apple 教程 (BirdSighting) 修改到我自己的应用程序中来为我的 iOS 项目学习良好的 MVC 实践。他们为模型和控制器构建了 NSObject 类。他们的第一个 ViewController 是一个 TableVC。在appDelegate.m中,他们 通过将 firstViewController 连接到 dataController 来更改 didFinishLaunchingWithOptions 。在我的应用程序中,我不希望我的第一个 ViewController 是一个表,而只是一个基本的 VC。我收到警告:不兼容的指针类型。这是代码:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
   UINavigationController *navigationController = (UINavigationController *)self.window.rootViewController;
   //  enterView is initial UIViewController
   enterView *firstViewController = (enterView *)[[navigationController viewControllers] objectAtIndex:0];
   //  dBcontrols is a NSObject class
   dBcontrols *aDataController = [[dBcontrols alloc] init];
   firstViewController.dataController = aDataController;   //  <-- ERROR Here.
   return YES;
}

我的第一个 ViewController enterView在标题中有这个:

@class contacts;
@class dBcontrols;
@interface enterView: UIViewController
@property (strong, nonatomic) enterView *dataController;

我的模型类、联系人和控制器、dBcontrols 实际上与 Apple 教程中的相同。但是 ViewController 没有访问 Controller。在enterView.m中有这些行:

#import "enterView.h"
#import "contacts.h"
#import "dBcontrols.h"

@interface enterView ()
@end

@synthesize dataController = _dataController;

- (void)viewWillAppear:(BOOL)animated {
   [super viewWillAppear:animated];
   NSInteger  cntC = [self.dataController countContacts];   <--  ERROR here
   NSLog(@"number of contacts = %d", cntC );

}

有一个错误说:No visible interface declarations selector 'countContacts' ,这是在dBcontrols.m中找到的控制器方法,如下所示:

- (NSUInteger)countContacts {
   return [self.masterContactList count];
}

这是标题中的内容,dBcontrols.h

@class contacts;
@interface dBcontrols: NSObject
   - (NSUInteger)countContacts;
   . . .
@end

我的问题是从 TableVC 切换到作为第一个 VC 的基本 VC 引起的吗?我认为这是本教程中唯一相关的更改。我该如何解决?我希望我已经提供了足够的信息。非常感谢!瑞克

4

2 回答 2

1

看起来你正在混淆你的课程。在您的应用程序委托中,您正在创建一个名为 aDataController 的 dBcontrols 实例,但在 enterView 的头文件中,您将 dataController 作为 enterView 类的一个实例——我想您可能是指那里的 dBcontrols。

顺便说一句,如果您坚持使用大写字母来开始类名的命名约定,您的代码会更容易阅读。

于 2013-07-05T04:40:44.233 回答
0

您需要检查以下两件事:

  • 确保在 enterView.m 中导入 dBcontrols.h

    #import "dBcontrols.h"

  • 确保在 dBcontrols.h 中声明了 countContacts

于 2013-07-05T02:22:25.047 回答