我正在尝试通过将我成功完成的Apple教程(BirdSighting)修改为自己的应用程序,来为我的iOS项目学习良好的MVC做法。他们为Model和Controller构建了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;
我的Model类,联系人和我的Controller 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 );
}
有一个错误提示:没有可见的接口声明选择器'countContacts',这是在dBcontrols.m中找到的Controller方法,如下所示:
- (NSUInteger)countContacts {
return [self.masterContactList count];
}
这是标题中的dBcontrols.h:
@class contacts;
@interface dBcontrols: NSObject
- (NSUInteger)countContacts;
. . .
@end
我的问题是由TableVC切换为基本VC作为第一个VC引起的吗?我认为这是本教程中唯一相关的更改。我该如何解决?希望我提供了足够的信息。
非常感谢!
里克
最佳答案
看来您正在混淆自己的课程。在您的应用程序委托中,您正在创建一个称为aDataController的dBcontrol实例,但是在enterView的头文件中,您将dataController作为enterView类的实例-我想您可能是在说dBcontrols。
顺便说一句,如果您坚持使用大写字母开头类名称的命名约定,那么您的代码将更易于阅读。
关于ios - 授予ViewController访问数据和 Controller 类的权限,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/17479995/