Objective-C - 从其他控制器访问方法
我对访问另一个控制器中的方法有一个小问题,我正在尝试这个。 例如,我有控制器 A 和 B。在控制器 A 中,我编写了一个方法,现在我想通过控制器 B 访问它。
我在头文件中的 A 类中做了什么:
+(void)goBack;
以及在实现文件中:
+(void)goBack {
NSLog(@"go back");
}
在控制器 B 中,我这样做是为了访问控制器 A 中的方法:
+(void)goPreviousArticle:(id)sender {
ViewProductInformation_ViewController *theInstance = [[ViewProductInformation_ViewController alloc] init];
[theInstance goBack];
}
但是,当我执行程序时,它不起作用,程序只是关闭,当我执行命令时,单击控制器 B 中的函数 goBack 我得到参考了控制器 A 中的方法。
有人知道问题出在哪里吗?
预先感谢,
雪
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这很简单......您只需混合类和实例方法声明:“+”号表示该方法是类方法。在你的情况下它应该是一个“-”所以
希望这会有所帮助。
类与实例方法声明...另请参阅类和实例方法之间有什么区别?
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
Hope this helps.
Class vs instance method declaration ... see also What is the difference between class and instance methods?
您将 goBack 声明为 CLASS 方法(带有前面的“+”)。将 + 更改为 -。
You are declaring goBack as a CLASS method (with the preceding "+"). Change the + to a -.
由于 goBack 是 A 类的静态方法,因此不需要 A 的实例来调用它的方法,您可以像这样调用它:
[ClassA goBack];
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];
您不需要声明静态函数,您可以像这样编写:
在类 A 中和类 B 中相同:
然后使用它们。我认为在这种情况下应用程序不会崩溃。
You don'y need to declare static functions you can writ like this:
In the class A and same in the class B:
Then use them. I think in that case application will not crashed.