简体   繁体   English

我如何在Objective-C的类之间工作?

[英]How do I work between classes in Objective-C?

At the moment, the majority of my code is in the same viewcontroller, and i'd like to move some of it over to other areas. 目前,我的大多数代码都在同一个ViewController中,我想将其中的一些移到其他区域。 Such as moving the animations all over to somewhere else. 例如将动画全部移动到其他地方。 But then how do i reference things which are in another class? 但是,我该如何引用另一类的东西呢? And how do i reference back from that class to items in my viewcontroller class? 以及如何从该类引用回viewcontroller类中的项目? Not going this has always disuaded me from doing it. 不这样做会一直使我无法这样做。

You might like to look into this here - to create static classes in objective c and then reference them in a separate file by classname - as in the view controller quoted in the linked example. 您可能希望在这里进行研究 -在目标c中创建静态类,然后按类名在单独的文件中引用它们-如链接示例中引用的视图控制器中所示。

Otherwise you can just create a new class within a separate .m file and then code it such that the calling method in another class will first create an instance of this new class and then invoke the necessary method on this instance. 否则,您可以只在一个单独的.m文件中创建一个新类,然后对其进行编码,以便另一个类中的调用方法将首先创建该新类的实例,然后在该实例上调用必要的方法。

Hope this helps. 希望这可以帮助。

there is a couple of ways you can achieve that. 有两种方法可以实现。 one way is the cocoa delegate @protocol way, the second way could be creating references to each object in the other class. 一种方法是可可委托@protocol方法,第二种方法是创建对另一个类中每个对象的引用。

for the first way you can do something like this: 对于第一种方式,您可以执行以下操作:

@class Class2;

@interface Class1 : NSObject {
    Class2 *cls2Pointer;

}

@property Class2 *cls2Pointer;
@end



@class Class1;

@interface Class2 : NSObject {
    Class1 *cls1Pointer;

}
@property Class1 *cls1Pointer;
@end



int main(){
    Class1 cls1Obj = [[Class1 alloc] init];
    Class2 cls2Obj = [[Class2 alloc] init];

    [cls1Obj setCls2Pointer:cls2Obj];
    [cls2Obj setCls1Pointer:cls1Obj];

}

the second way, is to declare a protocol in one/both of the classes to be able to pass arguments and call different methods on other objects: 第二种方法是在一个/两个类中声明一个协议,以便能够传递参数并在其他对象上调用不同的方法:

    @protocol Class1Delegate

- (void)class1:(Class1)obj MethodWithArg:(id)arg;

@end


@interface Class1 : NSObject {
    id <Class1Delegate> delegate;
}

@end



@interface Class2 : NSObject <Class1Delegate>{

}

@end

@implementation Class2

- (void)class1:(Class1)obj MethodWithArg:(id)arg {
        //do stuff when called from the 1st class
}
@end

Basically what you do is that you create one or more classes, move the code over to these classes and then create instances of these classes in your viewcontroller. 基本上,您要做的是创建一个或多个类,将代码移至这些类,然后在viewcontroller中创建这些类的实例

so if you had a method in your view controller 因此,如果您的视图控制器中有一个方法

-(void)foo;

you would create a new class say C and move the method there. 您将创建一个新的类C并在其中移动方法。

then in your view controller you would create an instance variable of that class eg 然后在您的视图控制器中,您将创建该类的实例变量,例如

C* myC;

then alloc/init and then call the foo method. 然后分配/初始化,然后调用foo方法。 This is not object oriented in the sense that foo is not really related to C in any way so method foo could have just been a static method not relating to the instance and as such called just like any other method but as [C foo] instead of [self foo] from the view controller. 这不是面向对象,因为foo在任何方面都不与C真正相关,因此foo方法可能只是一个与实例无关的静态方法,因此像其他方法一样被称为[C foo]来自视图控制器的[self foo]。

the other more OOP method would be to move functionality that belongs to together into a separate class like animation in your example. 另一种OOP方法是将属于一起的功能移动到一个单独的类中,例如您的示例中的animation。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM