Pregunta

I have declared my method 'callWEBservice()' in ViewController1.m and i want to call in ViewController2.m . I have created object of ViewController1.m in ViewController2.m as:

 ViewController1* mainVC = [[ViewController1 alloc] init]; 

Now i am trying to call that method but i am unable to do. Please help on this as I am new to iOS and I have searched some are saying to use delegates.

¿Fue útil?

Solución 2

First of all, don't use view controllers for this purpose, create a new class to handle methods of the same kind, then use that one across your view controllers. IF you want the SAME class to be shared across your program, then create a singleton.

How to call method from one class in another (iOS)

However, if you still want to do the view controller to view controller thing, the reason its not working is because you are instantiating a new view controller, not the one you were already using.

You have to pass the reference of the first VC to the second VC. It depends on how you are presenting the second VC. If you are using the Interface Builder, then you need to use:

How to pass prepareForSegue: an object

If you are manually creating and presenting the VC, before presenting it let it know which is the first VC.

You can use delegates like this:

How do I set up a simple delegate to communicate between two view controllers?

STILL consider redesigning your usage of the view controllers.

EDIT:

2 options,

1) Singleton:

Follow this guide http://www.galloway.me.uk/tutorials/singleton-classes/

2) AppDelegate:

Instantiate an object of the class in the .m of the app delegate and assign it to a property in the .h of the App Delegate. Then, retrieve this object.

This is an example of doing it with the motion manager from 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;

Otros consejos

You need to define the method signature in your .h file -

 - (void) callWebService;

and then in your .m file you define the method body:

- (void)callWebService
{
     // Whatever you need to do to call the web service
}

Then in ViewController2.m you can #import "ViewController1.h"

Now you can call [mainVC callWebService];

BUT The code you have shown creates a new instance of ViewController1 - If you already have an instance of ViewController1, such as the main view in your app, then this probably isn't what you wanted - you may need to set a property in ViewController2 and store a reference to your ViewController1

e.g. in ViewController2.h #import "ViewController1.h" // or use @class ViewController1 directive

@property (strong,nonatomic) ViewController1 *mainVC;

Then before in ViewController 1, before you present ViewController2 instance

vc2.mainVC=self;

Your invocation in ViewController2 then becomes

[self.mainVC callWebService];

At the risk of confusing you further, as a design note, it probably isn't best to have the callWebService method in a view controller. It might be more appropriate to create a singleton class for this purpose.

If you want to use methods from outside your class, you should declare them in your ViewController1.h file, not in the m, otherwise they are not visible (you could still call them using performSelector:withObject:afterDelay: but you should use the first solution)

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top