Reputation: 307
I've made a Model class called DBcontrols and I'm trying to use it in multiple views. (I'm still trying to learn proper MVC technique on iOS.) But the 2nd view, a TableVC, doesn't go to it. I'm pretty sure my problem lies in the app Delegate, here called 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 and listTableVC.h both have this code:
#import <UIKit/UIKit.h>
@class Contacts;
@class DBcontrols;
either: @interface enterView: UIViewController
or: @interface listTableVC: UITableViewController
@property (strong, nonatomic) DBcontrols *dataController;
. . .
@end
and that dataController is synthesized in both enterView.m and listTableVC.m
Here's the storyboard:
The Contacts TableVC, listTableVC, segues as a Push off of the List button on the enterView navigation bar.
All compiles successfully, but the DBcontrols methods are called in enterView, but not in listTableVC. For example, in both enterView and listTableVC I use the method 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];
}
When this is called from listTableVC, it never responds.
What am I doing wrong?
Thank you!
Upvotes: 0
Views: 196
Reputation: 70175
Within didFinishLaunchingWithOptions:
the secondViewController
won't exist yet. The controller is created lazily; that is, when needed and it is needed only when the transition from firstViewController is initiated.
In a Storyboard context you normally 'pass' values using
- (void) prepareForSegue: (UIStoryboardSegue *) segue sender: (id) sender
{
enterView *source = segue.sourceViewController;
listTableVc *target = segue.destinationViewController;
target.dataController = source.dataController;
}
Upvotes: 2
Reputation: 425
listTableVC *secondViewController = (listTableVC *)[[navigationController viewControllers] objectAtIndex:0];
Should be maybe:
listTableVC *secondViewController = (listTableVC *)[[navigationController viewControllers] objectAtIndex:1];
?
Upvotes: 0