0

I am having problems getting my application to build. I am trying to call a method from ViewController1 in my current ViewController2.

In my .h file of ViewController1 i have declared the method so it is visible.

-(void)saveLocation;

In my ViewController2.h file i have the following

    #import "ViewController1.h"



@interface ViewController2 : UIViewController

@property (nonatomic,assign) ViewController1 *MethodSave;

@end

and in my ViewController2.m

[self.MethodSave saveLocation];

it doesn't work and I've tried to fix it for a while now. Any help would be great. Thank you

iHulk
  • 4,869
  • 2
  • 30
  • 39
burrGGG
  • 617
  • 2
  • 9
  • 18
  • you have a typo here @property (nonatomic,assign) ViewController1 *MethodSave; It looks like MethodSave is a class name and so it is conflicting with it. try to change it with some other name. – Nofel Mahmood Oct 08 '14 at 14:09
  • It's actually called WeatherMethodSave, the error i am getting is saying 'Unknown type name 'ViewController1', i have imported the class? – burrGGG Oct 08 '14 at 14:14
  • You need to use delegation Go through this http://stackoverflow.com/questions/24815259/iphone-call-function-from-another-view-controller – Sarath Neeravallil Oct 08 '14 at 14:14
  • assign is used for primitive variable(int, BOOL). For objective C variable, strong or weak is used. I don't know if thats the issues. Try changing it. – Dipu Rajak Oct 08 '14 at 14:15
  • Are you sure about the correct spelling of the class: ViewController1.h? Did you rename that class after creating it and importing it? – vhristoskov Oct 08 '14 at 15:43

3 Answers3

0

I think you have only declared a property @property (nonatomic,assign) ViewController1 *MethodSave; but have not allocated this property. So first allocate it then call the function.

MethodSave = [ViewController1 new];
[self.MethodSave saveLocation];

also declare the property as strong not assign @property (nonatomic,strong) ViewController1 *MethodSave; this should work.

and try to declare variable with camel case.

iHulk
  • 4,869
  • 2
  • 30
  • 39
  • it's still giving me the same error, ViewController1 is unknown type name. Might i not have imported correctly. In ViewController2 i've #import "ViewController1". Thought that was all i needed? – burrGGG Oct 08 '14 at 14:37
  • @burrGGG I had updated my answer please try again and let me know if any problem. – iHulk Oct 08 '14 at 14:54
0

Are you also importing ViewController2 in your ViewController1? According to my own Google search your problem could be a circular dependency.

If this is the case, instead of importing ViewController1 in ViewController2, create a protocol in ViewController2.h and replace the ViewController1 property with an id that conforms to that protocol.

Add the following in ViewController2.h:

@protocol ViewController2Delegate<NSObject>
@required
-(void)saveLocation;
@end

@interface ViewController2:UIViewController
@property id<ViewController2Delegate> *delegate;
@end

and add this to ViewController1.h:

@interface ViewController1:UIViewController <ViewController2Delegate>

and then set the delegate to an instance of ViewController1 (if you're loading ViewController2 from ViewController1, it would be self), and be sure to remove the import from ViewController2.

I'm doing this by memory so it may not work copy and pasted, but hopefully this points you in the right direction.

mgiesa
  • 1,023
  • 7
  • 9
0

try this ,

ViewController1 *MethodSave = [[ViewController1 alloc]init];
[MethodSave saveLocation];
Devang Goswami
  • 284
  • 3
  • 23