ios - How to allow multiple views use the same controller class? -


i've made model class called dbcontrols , i'm trying use in multiple views. (i'm still trying learn proper mvc technique on ios.) 2nd view, tablevc, doesn't go it. i'm pretty sure problem lies in 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 , listtablevc.h both have code:

#import <uikit/uikit.h>  @class contacts; @class dbcontrols;  either:  @interface enterview: uiviewcontroller or:      @interface listtablevc: uitableviewcontroller  @property (strong, nonatomic) dbcontrols *datacontroller;    . . .  @end 

and datacontroller synthesized in both enterview.m , listtablevc.m

here's storyboard:

enter image description here

the contacts tablevc, listtablevc, segues push off of list button on enterview navigation bar.

all compiles successfully, dbcontrols methods called in enterview, not in listtablevc. example, in both enterview , listtablevc use method countcontacts:

- (nsuinteger)countcontacts {     nc = 0;     const char  *dbpath = [_databasepath utf8string];     if (sqlite3_open(dbpath, &_contactdb) == sqlite_ok) {         nsstring *querysql = [nsstring stringwithformat: @"select * 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 called listtablevc, never responds. doing wrong?
thank you!

within didfinishlaunchingwithoptions: secondviewcontroller won't exist yet. controller created lazily; is, when needed , needed when transition firstviewcontroller initiated.

in storyboard context 'pass' values using

- (void) prepareforsegue: (uistoryboardsegue *) segue sender: (id) sender {   enterview   *source = segue.sourceviewcontroller;   listtablevc *target = segue.destinationviewcontroller;    target.datacontroller = source.datacontroller; } 

Comments