【问题标题】:Giving ViewController access to Data and Controller Classes授予 ViewController 访问数据和控制器类的权限
【发布时间】:2013-07-05 01:42:25
【问题描述】:

我正在尝试通过将我已成功完成的 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 中的 Controller 方法,如下所示:

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

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

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

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

【问题讨论】:

    标签: ios xcode uiviewcontroller appdelegate


    【解决方案1】:

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

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

    【讨论】:

    • 完美!就是这样。感谢您的帮助和建议。
    【解决方案2】:

    你需要检查这两件事:

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

      #import "dBcontrols.h"

    • 确保在 dBcontrols.h 中声明 countContacts

    【讨论】:

    • 感谢您的回复,拉克莫。那些东西已经好了。我已经编辑了上面的问题来展示它们。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-12-20
    • 1970-01-01
    • 2016-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-15
    相关资源
    最近更新 更多