1

i have a little question about getting access to a method in another controller, nu i am trying this. So for example i have the controller A and B. In the controller A i have programmed a method, now i want to get access this through controller B.

What i have done in class A in the header file:

+(void)goBack;

and in the implementation file:

+(void)goBack {
NSLog(@"go back");
}

in the controller B i do this to get access to the method in controller A:

+(void)goPreviousArticle:(id)sender {
ViewProductInformation_ViewController *theInstance = [[ViewProductInformation_ViewController alloc] init];
[theInstance goBack];
}

However when i execute the program, then it does not work, the program just shuts down, when i do command click on the function goBack in controller B i get referred to the method in controller A.

Does anybody have an idea what the problem could be?

thanks in advance,

snowy

John Parker
  • 54,048
  • 11
  • 129
  • 129
Snowy
  • 139
  • 1
  • 2
  • 9

4 Answers4

3

It's quite easy ... you just mixed the class and instance-method declaration: The "+" sign indicates that the method is a class method. In your case it should be a "-" so

-(void)goBack; // a instance method declaration!

Hope this helps.

Class vs instance method declaration ... see also What is the difference between class and instance methods?

Community
  • 1
  • 1
crosscode
  • 1,022
  • 8
  • 12
  • this surely helps ! i am glad after many hours of searching that you could give me a solution. also thank you for the explanation, it has cleared out a lot for me ! – Snowy May 02 '11 at 14:45
0

You are declaring goBack as a CLASS method (with the preceding "+"). Change the + to a -.

Rayfleck
  • 12,116
  • 8
  • 48
  • 74
0

Since goBack is a static method of Class A, you don't need an instance of A to call it's method, you can just call it like so:

[ClassA goBack];

Todd Hopkinson
  • 6,803
  • 5
  • 32
  • 34
0

You don'y need to declare static functions you can writ like this:

-(void)goBack {
NSLog(@"go back");
}

In the class A and same in the class B:

-(void)goPreviousArticle:(id)sender {
ViewProductInformation_ViewController *theInstance = [[ViewProductInformation_ViewController alloc] init];
[theInstance goBack];
}

Then use them. I think in that case application will not crashed.

Viktor Apoyan
  • 10,655
  • 22
  • 85
  • 147