开发者

Objective-C - Access method from other controller

i have a little question about getting access to a method in another controller, nu i am trying th开发者_如何学JAVAis. 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


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?


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


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];


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜