【问题标题】:How to allow multiple views use the same controller class?如何允许多个视图使用同一个控制器类?
【发布时间】: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.hlistTableVC.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.mlistTableVC.m

中合成的

这是故事板:

联系人 TableVC,listTableVC,作为 Push 离开 enterView 导航上的 List 按钮吧。

全部编译成功,但是enterView中调用了DBcontrols方法,而listTableVC中没有。例如,在 enterViewlistTableVC 中,我都使用 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


【解决方案1】:

didFinishLaunchingWithOptions: 内,secondViewController 尚不存在。控制器是惰性创建的;也就是说,在需要时,并且仅在从 firstViewController 开始转换时才需要。

在情节提要上下文中,您通常使用

“传递”值
- (void) prepareForSegue: (UIStoryboardSegue *) segue sender: (id) sender
{
  enterView   *source = segue.sourceViewController;
  listTableVc *target = segue.destinationViewController;

  target.dataController = source.dataController;
}

【讨论】:

    【解决方案2】:
    listTableVC *secondViewController = (listTableVC *)[[navigationController viewControllers] objectAtIndex:0];
    

    应该是:

    listTableVC *secondViewController = (listTableVC *)[[navigationController viewControllers] objectAtIndex:1];
    

    ?

    【讨论】:

      猜你喜欢
      • 2017-11-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-21
      • 1970-01-01
      • 2017-11-01
      • 1970-01-01
      • 2014-01-08
      相关资源
      最近更新 更多