ios - call method from other class (self issue) -


i know proper solution problem…if in myotherclass need call method in mymainclass example know how:

mymainclass *class = [mymainclass alloc] init]; [class runsomemethod]; 

the problem if in runsomemethod refer self different self because different instance called myotherclass. understand (and know how workaround it), please educate me on right way handle , call original instance of mymainclass if need be. thanks!

instead of creating instance of own class, can use self object call method.

then when refer self in method, using same instance.

so, should call method

[self runsomemethod]; 

alternatively, if want class refer 1 object @ times, may want consider creating class singleton class, i.e., no matter - class issue 1 object.

singleton way

to create singleton class, go mymainclass.h , add property:

+ (mymainclass *)singletoninstance; 

then in implementation file, mymainclass.m add in following code:

//just below @implementation mymainclass static mymainclass* _singletoninstance = nil;  +(mymainclass*)singletoninstance { @synchronized([mymainclass class]) {     if (!_singletoninstance)         _singletoninstance = [[self alloc] init];     return _singletoninstance; }  return nil; }   +(id)alloc { @synchronized([mymainclass class]) {     nsassert(_singletoninstance == nil, @"attempted allocate second instance of singleton.");     _singletoninstance = [super alloc];     return _singletoninstance; }  return nil; } 

that it. whenever want call mymainclass's object, use [[mymainclass singletoninstance] runsomemethod]. also, when use self now, refer same object.


Comments

Popular posts from this blog

javascript - DIV "hiding" when changing dropdown value -

Does Firefox offer AppleScript support to get URL of windows? -

android - How to install packaged app on Firefox for mobile? -