iphone - How to create a static delegate from main-viewcontroller? -


hope can me on learning new stuff delegates in ios-programming.

i have "mainviewcontroller" first vc when app starts. i´ve kind of modelselection different uiimageviews , after choosing 1 of them, i´m pushing new vc. want handle modelchoice delegate, other viewcontrollers can listen , act based on users choice.

but mean have alloc new instance of "mainviewcontroller" in every vc? whats solution on that? how create (i think called) static delegate?

would great learn that.. sharing..

you can make mainviewcontroller singleton class:

static  mainviewcontroller* ctrl = nil; +(mainviewcontroller*) sharedinstance {    @synchronized( ctrl ) {        if( !ctrl ) {            ctrl = [[mainviewcontroller alloc] init];        }    }     return ctrl; } 

then can access class this: [mainviewcontroller sharedinstance].

you can add method mainviewcontroller like:

-(void) addlistener:(nsobject<listenerprotocol>*) listener; 

and when you're creating other view controllers call:

[[mainviewcontroller sharedinstance] addlistener: newvc]; 

Comments

Popular posts from this blog

Javascript line number mapping -

c# - Is it possible to remove an existing registration from Autofac container builder? -

php - Mysql PK and FK char(36) vs int(10) -