xcode 如何从另一个类文件调用函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2696942/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to call a function from another class file
提问by Guy Parker
I am very familiar with writing VB based applications but am new to Xcode (and Objective C). I have gone through numerous tutorials on the web and understand the basics and how to interact with Interface Builder etc. However, I am really struggling with some basic concepts of the C language and would be grateful for any help you can offer. Heres my problem…
我非常熟悉编写基于 VB 的应用程序,但我是 Xcode(和 Objective C)的新手。我已经浏览了网络上的大量教程,了解基础知识以及如何与 Interface Builder 等交互。但是,我真的很纠结于 C 语言的一些基本概念,如果您能提供任何帮助,我将不胜感激。这是我的问题…
I have a simple iphone app which has a view controller (FirstViewController) and a subview (SecondViewController) with associated header and class files.
我有一个简单的 iphone 应用程序,它有一个视图控制器(FirstViewController)和一个带有相关头文件和类文件的子视图(SecondViewController)。
In the FirstViewController.m have a function defined
在 FirstViewController.m 中定义了一个函数
@implementation FirstViewController
- (void) writeToServer:(const uint8_t *) buf {
[oStream write:buf maxLength:strlen((char*)buf)];
}
It doesn't really matter what the function is.
功能是什么并不重要。
I want to use this function in my SecondViewController, so in SecondViewController.m I import FirstViewController.h
我想在我的 SecondViewController 中使用这个函数,所以在 SecondViewController.m 我导入 FirstViewController.h
#import "SecondViewController.h"
#import "FirstViewController.h"
@implementation SecondViewController
-(IBAction) SetButton: (id) sender {
NSString *s = [@"Fill:" stringByAppendingString: FillLevelValue.text];
NSString *strToSend = [s stringByAppendingString: @":"];
const uint8_t *str = (uint8_t *) [strToSend cStringUsingEncoding:NSASCIIStringEncoding];
FillLevelValue.text = strToSend;
[FirstViewController writeToServer:str];
}
This last line is where my problem is. XCode tells me that FirstViewController may not respond to writeToServer. And when I try to run the application it crashes when this function is called.
最后一行是我的问题所在。XCode 告诉我 FirstViewController 可能不会响应 writeToServer。当我尝试运行该应用程序时,它会在调用此函数时崩溃。
I guess I don't fully understand how to share functions and more importantly, the relationship between classes.
我想我没有完全理解如何共享函数,更重要的是,类之间的关系。
In an ideal world I would create a global class to place my functions in and call them as required.
在理想的世界中,我会创建一个全局类来放置我的函数并根据需要调用它们。
Any advice gratefully received.
感激地收到任何建议。
回答by gammelgul
In the deklaration of your method:
在您的方法的 deklaration 中:
- (void) writeToServer:(const uint8_t *) buf;
The - at the beginning of the declaration indicates, that it is an instant method. This means, you have to create an instance of your class to call it. If there were a +, the method would be a class method and you could call it the way you do in your code.
声明开头的 - 表示它是一个即时方法。这意味着,您必须创建类的一个实例来调用它。如果有一个 +,则该方法将是一个类方法,您可以按照您在代码中的方式调用它。
Example:
例子:
@interface MyClass
{ }
- (void) myInstanceMethod;
+ (void) myClassMethod;
@end
Then in some function:
然后在一些函数中:
[MyClass myClassMethod]; // This is ok
//[MyClass myInstanceMethod]; // this doenst't work, you need an instance:
MyClass *theInstance = [[MyClass alloc] init];
[theInstance myInstanceMethod];
回答by Steve Harrison
In the line:
在行中:
[FirstViewController writeToServer:str];
...you are sending the message writeToServer:
to the FirstViewController
class. Instead, you need to send it to a FirstViewController
object. This means that you need to obtain a reference to a FirstViewController
object and then send the objectthe writeToServer:
message:
...您要发送的消息writeToServer:
的FirstViewController
类。相反,您需要将其发送到FirstViewController
object。这意味着你需要获得一个参考FirstViewController
对象,然后发送对象的writeToServer:
消息:
[firstViewControllerObject writeToServer:str];
However, because there is only one view active at a time on the iPhone, you should not be having FirstViewController
and SecondViewController
objects existing at the same time and communicating with each other. Instead, you should create independent classes to perform the required class and the active view controller should be interacting with these classes.
但是,因为 iPhone 上一次只有一个视图处于活动状态,所以您不应该让FirstViewController
和SecondViewController
对象同时存在并相互通信。相反,您应该创建独立的类来执行所需的类,并且活动视图控制器应该与这些类进行交互。
Note: "You are sending the message sayHello:
to the object myObject
" is just another way of saying "you are calling the writeToServer:
method of the object myObject
. (A "method" is like the object-oriented version of a function.)
注意:“您正在向sayHello:
对象发送消息myObject
”只是“您正在调用writeToServer:
对象的方法”的另一种说法myObject
。(“方法”就像函数的面向对象版本。)
I would recommend reading the Learning Objective-Cand Learn Cocoa IItutorials on Cocoa Dev Centralto learn more about Cocoa/Objective-C and object-oriented programming.
我建议阅读Cocoa Dev Central上的Learning Objective-C和Learn Cocoa II教程,以了解有关 Cocoa/Objective-C 和面向对象编程的更多信息。
回答by gnasher
In your
FirstViewController.h
file, you must declarewriteToServer
as a method ofFirstViewController
object in the @interface section@interface FirstViewController : UIViewController { } - (void) writeToServer:(NSString *) str; @end
Secondly, the following message you send is invalid as
FirstViewController
writeToServer
is not a class method and is only valid if called from an instantiation of theFirstViewController
class.FirstViewController *firstViewCnt = [[FirstViewController alloc] init]; [firstViewCnt writeToServer:str]; [firstViewCnt release];
在您的
FirstViewController.h
文件中,您必须在@interface 部分声明writeToServer
为FirstViewController
对象的方法@interface FirstViewController : UIViewController { } - (void) writeToServer:(NSString *) str; @end
其次,您发送的以下消息无效,因为
FirstViewController
writeToServer
它不是类方法,并且仅在从FirstViewController
类的实例化调用时才有效。FirstViewController *firstViewCnt = [[FirstViewController alloc] init]; [firstViewCnt writeToServer:str]; [firstViewCnt release];
Or something like that.
或类似的东西。
回答by Mihai Damian
You need to write:
你需要写:
- (void) writeToServer:(const uint8_t *) buf;
in your FirstViewController.h file. This is a hint for the compiler that the function is available for that particular class. Your code would still work without this, because the actual function is implemented, but you get the warning because the compiler doesn't check the .m file for existing functions when you include the header from another source file.
在您的 FirstViewController.h 文件中。这是对编译器的提示,该函数可用于该特定类。如果没有这个,您的代码仍然可以工作,因为实际的函数已经实现,但是您会收到警告,因为当您包含另一个源文件的头文件时,编译器不会检查 .m 文件中的现有函数。
回答by Mihir Mehta
if you want to call this method like this only declare writeToServer method as class method (static method in short...)
如果你想像这样调用这个方法,只需将 writeToServer 方法声明为类方法(简称静态方法......)
for that you have declare and define it like
为此,您已经声明并定义了它
+ (void) writeToServer:(const uint8_t *) buf;
+ (void) writeToServer:(const uint8_t *) buf {
[oStream write:buf maxLength:strlen((char*)buf)];
}
only difference is +sign instead of -
唯一的区别是+号而不是-
- sign indicate that method is class method(static) and - sign is for instance method (non static)
- 符号表示方法是类方法(静态), - 符号表示实例方法(非静态)
Second optionis creat instance of FirstViewController and call the existing method
第二个选项是创建 FirstViewController 的实例并调用现有方法
i.e
IE
FirstViewController FirstViewControllerObj = [[FirstViewController alloc] init];
[FirstViewControllerObj writeToServer:str];
回答by Nischal Hada
FirstViewController.h
第一视图控制器.h
@interface FirstViewController: UIViewController
- (void)GetItNow;
FirstViewController.m
第一视图控制器.m
- (void)GetItNow{
NSLog(@"I acheived"); }
- (IBAction)goToSecondView:(id)sender {
SecondViewController* Second= [[SecondViewControlleralloc] initWithNibName:@"SecondViewController" bundle:nil];
rqVC.addId = self.addId;
[self.view addSubview:Second.view];
}
SecondViewController.h
第二视图控制器.h
@property (nonatomic, assign) id delegate;
SecondViewController.m
第二视图控制器.m
- (IBAction)Action_LoadFunds:(id)sender {
[self.view removeFromSuperview];
[_delegate GetItNow];
}