如何从另一个类中的一个类调用方法(iOS)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9731126/
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 method from one class in another (iOS)
提问by Matt Ledger
This a very basic question but I've searched all over and been unable to find an answer that explains well enough for me to get my head around it.
这是一个非常基本的问题,但我已经搜索了所有内容,但无法找到一个解释得足够好让我理解它的答案。
What I want to do is create a method in one class of my iOS app and then call that method from other classes in my app. Could someone explain exactly what I need to do to achieve this? Any help would be greatly appreciated as all my attempts so far have failed!
我想要做的是在我的 iOS 应用程序的一个类中创建一个方法,然后从我的应用程序中的其他类调用该方法。有人可以确切地解释我需要做什么来实现这一目标吗?任何帮助将不胜感激,因为到目前为止我所有的尝试都失败了!
Thanks.
谢谢。
回答by Pochi
Objective-C:
目标-C:
You have to import the header of the class that contains the method you want to use (ClassYouWantToUse.h) into the class you want to use it at (TargetClass).
您必须将包含要使用的方法的类的标头 ( ClassYouWantToUse.h) 导入到要在 ( TargetClass)使用它的类中。
Inside the TargetClass.h or TargetClass.m (depending on the scope you want to give it):
在 TargetClass.h 或 TargetClass.m 中(取决于你想给它的范围):
#import "ClassYouWantToUse.h"
Then create an instance of the class you want to use inside the target class either as a property like this:
然后创建一个你想在目标类中使用的类的实例,作为这样的属性:
@property (nonatomic,strong) ClassYouWantToUse *classObject;
Or as an instance variable like this:
或者作为这样的实例变量:
ClassYouWantToUse *classObject;
Make sure you initialize it! (usually inside ViewDidLoad):
确保你初始化它!(通常在 ViewDidLoad 中):
classObject = [[ClassYouWantToUse alloc] init];
Now you can call any public methods from that class like this:
现在您可以像这样从该类调用任何公共方法:
[classObject theClassMethodWithParam:param1 andSecondParam:param2];
Note:The ClassYouWantToUse class must have the methods that you want to make accessible to others by declaring them in the header file:
注意:ClassYouWantToUse 类必须具有您希望通过在头文件中声明其他人可以访问的方法:
- (void)theClassMethodWithParam:(UIImage*)someImage andSecondParam:(NSString*)someText;
Otherwise you won't be able to see these methods.
否则,您将无法看到这些方法。
Swift:
迅速:
Theres really nothing special about it in swift, just adding this as a reference.
在 swift 中确实没有什么特别之处,只是将其添加为参考。
In swift you simply create an instance of the class you want to use:
在 swift 中,您只需创建要使用的类的实例:
let classObject = ClassYouWantToUse()
And use it directly:
并直接使用它:
classObject.theClassMethodWithParam(param1, andSecondParam:param2)
回答by aroth
You have two basic options. You can either create or pass-in an instance of the first class to the second class, or you can add a static method to the first class and call it directly using the class object.
您有两个基本选项。您可以创建或将第一个类的实例传递给第二个类,也可以向第一个类添加静态方法并使用类对象直接调用它。
For instance, say you have:
例如,假设您有:
@interface ClassA : NSObject {
}
//instance methods
- (int) addNumber:(int)num1 withNumber:(int)num2;
//static/class methods
+ (int) add:(int)num1 with:(int)num2;
@end
@implementation ClassA
- (int) addNumber:(int)num1 withNumber:(int)num2 {
return num1 + num2;
}
+ (int) add:(int)num1 with:(int)num2 {
return num1 + num2;
}
@end
Then you can do:
然后你可以这样做:
#import "ClassA.h"
@interface ClassB : NSObject {
ClassA* adder;
}
//constructors
- (id) init; //creates a new instance of ClassA to use
- (id) initWithAdder:(ClassA*)theAdder; //uses the provided instance of ClassA
//instance methods
- (int) add2To:(int)num;
//static/class methods
+ (int) add3To:(int)num;
@end
@implementation ClassB
- (id) init {
if (self = [super init]) {
adder = [[ClassA alloc] init];
}
return self;
}
- (id) initWithAdder:(ClassA*)theAdder {
if (self = [super init]) {
adder = theAdder;
}
return self;
}
- (int) add2To:(int)num {
return [adder addNumber:2 withNumber:num];
}
+ (int) add3To:(int)num {
return [ClassA add:3 with:num];
}
@end
Note that in most cases, you would use instance methods rather than static methods.
请注意,在大多数情况下,您将使用实例方法而不是静态方法。
回答by anticyclope
You have to use the concept of delegation.
您必须使用委托的概念。