【发布时间】:2013-07-10 06:22:01
【问题描述】:
我创建了一个名为 DBcontrols 的模型类,并尝试在多个视图中使用它。 (我仍在尝试在 iOS 上学习正确的 MVC 技术。)但是第二个视图 TableVC 并没有涉及到它。我很确定我的问题在于应用程序 Delegate,这里称为 dBAppDelegate.m:
#import "dBAppDelegate.h"
// Controller Class
#import "DBcontrols.h"
// View Classes
#import "enterView.h"
#import "listTableVC.h"
@implementation dBAppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
UINavigationController *navigationController = (UINavigationController *)self.window.rootViewController;
enterView *firstViewController = (enterView *)[[navigationController viewControllers] objectAtIndex:0];
listTableVC *secondViewController = (listTableVC *)[[navigationController viewControllers] objectAtIndex:0];
DBcontrols *aDataController = [[DBcontrols alloc] init];
firstViewController.dataController = aDataController;
secondViewController.dataController = aDataController;
return YES;
}
enterView.h 和 listTableVC.h 都有这样的代码:
#import <UIKit/UIKit.h>
@class Contacts;
@class DBcontrols;
either: @interface enterView: UIViewController
or: @interface listTableVC: UITableViewController
@property (strong, nonatomic) DBcontrols *dataController;
. . .
@end
dataController 是在 enterView.m 和 listTableVC.m
中合成的这是故事板:
联系人 TableVC,listTableVC,作为 Push 离开 enterView 导航上的 List 按钮吧。
全部编译成功,但是enterView中调用了DBcontrols方法,而listTableVC中没有。例如,在 enterView 和 listTableVC 中,我都使用 countContacts 方法:
- (NSUInteger)countContacts {
nC = 0;
const char *dbpath = [_databasePath UTF8String];
if (sqlite3_open(dbpath, &_contactDB) == SQLITE_OK) {
NSString *querySQL = [NSString stringWithFormat: @"SELECT * FROM contacts"];
const char *query_stmt = [querySQL UTF8String];
if (sqlite3_prepare_v2(_contactDB, query_stmt, -1, &statement, NULL) == SQLITE_OK) {
while (sqlite3_step(statement) == SQLITE_ROW) {
nC++;
}
}
}
NSLog(@"%d contacts in dB.", nC );
return [self.masterContactList count];
}
当从 listTableVC 调用它时,它从不响应。
我做错了什么?
谢谢!
【问题讨论】:
-
使用调试器。在 countContacts 和/或 viewWillAppear 中设置断点。确认两个控制器中
dataController的值。 -
是的,这就是问题所在。在 enterView 中,dataController 会返回所有正确的值。单击 LIST 按钮并转到 listTableVC 后,dataController 变空了。
标签: ios xcode model-view-controller delegates uitableview