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.
任何建议都感激不尽。
6 个解决方案
#1
31
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];
#2
3
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 object the writeToServer:
message:
...您正在将消息writeToServer:发送到FirstViewController类。相反,您需要将其发送到FirstViewController对象。这意味着您需要获取对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”只是另一种说法“你正在调用对象myObject的writeToServer:方法。”(“方法”就像一个函数的面向对象版本。 )
I would recommend reading the Learning Objective-C and Learn Cocoa II tutorials on Cocoa Dev Central to learn more about Cocoa/Objective-C and object-oriented programming.
我建议阅读Cocoa Dev Central上的Learning Objective-C和Learn Cocoa II教程,以了解有关Cocoa / Objective-C和面向对象编程的更多信息。
#3
2
-
In your
FirstViewController.h
file, you must declarewriteToServer
as a method ofFirstViewController
object in the @interface section在FirstViewController.h文件中,必须将writeToServer声明为@interface部分中FirstViewController对象的方法
@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 writeToServer不是类方法,并且仅在从FirstViewController类的实例化调用时才有效。
FirstViewController *firstViewCnt = [[FirstViewController alloc] init]; [firstViewCnt writeToServer:str]; [firstViewCnt release];
Or something like that.
或类似的东西。
#4
1
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文件中的现有函数。
#5
0
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)
sign表示该方法是类方法(静态),而 - sign是例如方法(非静态)
Second option is creat instance of FirstViewController and call the existing method
第二个选项是FirstViewController的creat实例并调用现有方法
i.e
FirstViewController FirstViewControllerObj = [[FirstViewController alloc] init];
[FirstViewControllerObj writeToServer:str];
#6
0
FirstViewController.h
@interface FirstViewController: UIViewController
- (void)GetItNow;
FirstViewController.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
@property (nonatomic, assign) id delegate;
SecondViewController.m
- (IBAction)Action_LoadFunds:(id)sender {
[self.view removeFromSuperview];
[_delegate GetItNow];
}
#1
31
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];
#2
3
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 object the writeToServer:
message:
...您正在将消息writeToServer:发送到FirstViewController类。相反,您需要将其发送到FirstViewController对象。这意味着您需要获取对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”只是另一种说法“你正在调用对象myObject的writeToServer:方法。”(“方法”就像一个函数的面向对象版本。 )
I would recommend reading the Learning Objective-C and Learn Cocoa II tutorials on Cocoa Dev Central to learn more about Cocoa/Objective-C and object-oriented programming.
我建议阅读Cocoa Dev Central上的Learning Objective-C和Learn Cocoa II教程,以了解有关Cocoa / Objective-C和面向对象编程的更多信息。
#3
2
-
In your
FirstViewController.h
file, you must declarewriteToServer
as a method ofFirstViewController
object in the @interface section在FirstViewController.h文件中,必须将writeToServer声明为@interface部分中FirstViewController对象的方法
@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 writeToServer不是类方法,并且仅在从FirstViewController类的实例化调用时才有效。
FirstViewController *firstViewCnt = [[FirstViewController alloc] init]; [firstViewCnt writeToServer:str]; [firstViewCnt release];
Or something like that.
或类似的东西。
#4
1
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文件中的现有函数。
#5
0
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)
sign表示该方法是类方法(静态),而 - sign是例如方法(非静态)
Second option is creat instance of FirstViewController and call the existing method
第二个选项是FirstViewController的creat实例并调用现有方法
i.e
FirstViewController FirstViewControllerObj = [[FirstViewController alloc] init];
[FirstViewControllerObj writeToServer:str];
#6
0
FirstViewController.h
@interface FirstViewController: UIViewController
- (void)GetItNow;
FirstViewController.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
@property (nonatomic, assign) id delegate;
SecondViewController.m
- (IBAction)Action_LoadFunds:(id)sender {
[self.view removeFromSuperview];
[_delegate GetItNow];
}