ios - Calling method declared in ViewController1.m from another ViewController2.m file -
i have declared method 'callwebservice()' in viewcontroller1.m , want call in viewcontroller2.m . have created object of viewcontroller1.m in viewcontroller2.m as:
viewcontroller1* mainvc = [[viewcontroller1 alloc] init]; now trying call method unable do. please on new ios , have searched saying use delegates.
first of all, don't use view controllers purpose, create new class handle methods of same kind, use 1 across view controllers. if want same class shared across program, create singleton.
how call method 1 class in (ios)
however, if still want view controller view controller thing, reason not working because instantiating new view controller, not 1 using.
you have pass reference of first vc second vc. depends on how presenting second vc. if using interface builder, need use:
how pass prepareforsegue: object
if manually creating , presenting vc, before presenting let know first vc.
you can use delegates this:
how set simple delegate communicate between 2 view controllers?
still consider redesigning usage of view controllers.
edit:
2 options,
1) singleton:
follow guide http://www.galloway.me.uk/tutorials/singleton-classes/
2) appdelegate:
instantiate object of class in .m of app delegate , assign property in .h of app delegate. then, retrieve object.
this example of doing motion manager ios:
appdelegate.h:
@property (strong,nonatomic) cmmotionmanager *motionmanager; appdelegate.m
_motionmanager = [[cmmotionmanager alloc] init]; viewcontroller1-2-etc
cmmotionmanager *motionmanager; motionmanager = ((appdelegate*)[uiapplication sharedapplication].delegate).motionmanager;
Comments
Post a Comment