从其他类 Objective-C 访问方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1658433/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 22:26:58  来源:igfitidea点击:

Accessing Method from other Classes Objective-C

iphoneobjective-cclassobjectmethods

提问by Ethan Mick

Looked for an answer for this question, but I haven't found a suitable one yet. I'm hoping you guys (and gals) can help me out! (This is for an iPhone app)

寻找这个问题的答案,但我还没有找到合适的答案。我希望你们(和女孩们)可以帮助我!(这是针对 iPhone 应用程序的)

Alright, I have a Mutliview application. Each view has it's own class, and everything is happy. However, the different classes sometimes call the same method. Up until now, I have simply wrote that Method twice, in both of the class files.

好的,我有一个 Mutliview 应用程序。每个视图都有自己的类,一切都很愉快。但是,不同的类有时会调用相同的方法。到目前为止,我只是在两个类文件中两次编写了该方法。

This is what I want to do though:

不过,这就是我想要做的:

I want to make a new class, in It's own file, that has all the "Common" Methods. Then, whenever another class needs to call the Method, I simply call it from the other file. This way, when I want to change the Method, I only need to change it in one place, and not all the places...

我想在它自己的文件中创建一个新类,该类包含所有“通用”方法。然后,每当另一个类需要调用该方法时,我只需从另一个文件中调用它。这样,当我想改变Method时,我只需要在一个地方改变它,而不是所有的地方......

I'm not sure how I'd do this, which is why I'm asking for help. I'm a little rusty and new for Objective-C, so pretty examples will help me a lot. Allow me to give you one.

我不确定我会怎么做,这就是我寻求帮助的原因。我对Objective-C有点生疏和陌生,所以漂亮的例子会对我有很大帮助。请允许我给你一个。

File: ViewController1.m

文件:ViewController1.m

@implementation ViewController1

//Do Some awesome stuff....

CALL "CommonMethod" HERE

@end

File: ViewController2.m

文件:ViewController2.m

@implementation ViewController2

//Do Some awesome stuff....

CALL "CommonMethod" HERE

@end

File: CommonClass

文件:CommonClass

@implementation commonClass

- (void)CommonMethod:(id)sender
{

//So some awesome generic stuff...



    }
@end

I feel like I need to #import the other file, make an Object from the class and call the Method from the Object... How do I do that?

我觉得我需要#import 另一个文件,从类中创建一个对象并从对象中调用方法......我该怎么做?

Thanks again!

再次感谢!

回答by Nir Levy

Option 1:

选项1:

@implementation commonClass
+ (void)CommonMethod:(id)sender  /* note the + sign */
{
//So some awesome generic stuff...
    }
@end

@implementation ViewController2

- (void)do_something... {
    [commonClass CommonMethod];
}


@end

Option 2:

选项 2:

@implementation commonClass
- (void)CommonMethod:(id)sender
{
//So some awesome generic stuff...
    }
@end

@implementation ViewController2

- (void)do_something... {
    commonClass *c=[[commonClass alloc] init];
    [c CommonMethod];
    [c release];
}

@end

Option 3: use inheritance (see Mr. Totland's description in this thread)

选项 3:使用继承(请参阅此线程中 Totland 先生的描述)

@implementation commonClass
- (void)CommonMethod:(id)sender
{
//So some awesome generic stuff...
    }
@end

/* in your .h file */
@interface ViewController2: commonClass

@end

naturally you always need to #import commonClass.hin your view controllers..

自然你总是需要#import commonClass.h在你的视图控制器中..

回答by bpapa

There are some answers here telling you to create a common "parent" class. However I think that you can do a lot better. Create a categoryfor UIViewController instead. You don't know all of the internals of what is going on with UIViewController so I don't think it is worth creating your own View Controller hierarchy off of. In fact it could be dangerous. I ran into a number of problems when I tried to create a "base" UITableViewController and then create classes that inherit from that. I avoided these problems by using categories instead.

这里有一些答案告诉您创建一个通用的“父”类。但是我认为你可以做得更好。而是为 UIViewController创建一个类别。您不了解 UIViewController 发生的所有内部情况,因此我认为不值得创建您自己的 View Controller 层次结构。事实上,这可能是危险的。当我尝试创建一个“基础” UITableViewController 然后创建从它继承的类时遇到了许多问题。我通过使用类别来避免这些问题。

Your #1 priority shouldn't be inheriting things for no good reason, it should be getting an app into the app store that people will want to download.

您的第一要务不应该是无缘无故地继承东西,而应该是让人们想要下载的应用程序进入应用程序商店。

回答by Carl Norum

It sounds to me like the common code doesn't need to be in a class at all. Is there a reason you can't just use a C-style function for what you want to do?

在我看来,公共代码根本不需要在类中。您是否有理由不能只使用 C 风格的函数来完成您想做的事情?

You could put the common code in a class and then make your other two classes subclasses of that one; this method also avoids the code duplication.

你可以把公共代码放在一个类中,然后让你的另外两个类成为那个类的子类;这种方法也避免了代码重复。

Another option might be to write a class method instead of instance methods for this common code. I think most people feel that singletons are best avoided as a design choice.

另一种选择可能是为此公共代码编写类方法而不是实例方法。我认为大多数人认为最好避免单例作为设计选择。

It would be easier to give a good answer if we knew more about what you were really trying to accomplish.

如果我们对您真正想要实现的目标有更多了解,那么给出好的答案会更容易。

回答by Williham Totland

What you want to do is to make the two controllers share a common superclass:

你要做的是让两个控制器共享一个共同的超类:

UIViewController : MyAwesomeViewController : ViewController1
                                           : ViewController2

commonMethod:would then reside in MyAwesomeViewController. Also, don't start method names with capital letters. :)

commonMethod:然后将驻留在 MyAwesomeViewController 中。另外,不要以大写字母开头方法名称。:)

To elaborate:

详细说明:

+@interface MyAwesomeController : UIViewController {

-@interface ViewController1 : UIViewController { // and ditto for ViewController2
+@interface ViewController1 : MyAwesomeController {

回答by AlBlue

Bear in mind that Objective-C is just a superset of C, and that whilst #include directives are mostly used for header files, there's nothing stopping you using a #include to embed the contents of one implementation inside another implementation. If the code is truly identical, you can easily just stick it in its own file, and #include it in the .m file.

请记住,Objective-C 只是 C 的超集,虽然 #include 指令主要用于头文件,但没有什么能阻止您使用 #include 将一个实现的内容嵌入另一个实现中。如果代码确实相同,您可以轻松地将其粘贴在自己的文件中,并在 .m 文件中 #include 。

Having said that, perhaps it would be better to use this technique in conjunction with categories, especially if the same implementation has similar behaviours.

话虽如此,也许将此技术与类别结合使用会更好,特别是如果相同的实现具有相似的行为。

回答by Niels Castle

Pass in a reference to your commonClass when you alloc and init your views...

当您分配和初始化您的视图时,传递对您的 commonClass 的引用......

CommonClass *cc = [[CommonClass alloc] init];

ViewController1 *vc1 = [[ViewController1 alloc] ... initWith:cc];
ViewController2 *vc2 = [[ViewController2 alloc] ... initWith:cc];

but making a classic c include might suffice.

但是制作一个经典的 c include 可能就足够了。

回答by David

As an iPhone neophyte with a Java background and little C, I had a similar problem wishing to refer to a method in both the RootController and a ViewController. It seemed to me that the proper place for the method was the AppDelegate class, an instance of which one obtains in other classes by:

作为一个有 Java 背景和小 C 的 iPhone 新手,我有一个类似的问题,希望在 RootController 和 ViewController 中都引用一个方法。在我看来,该方法的正确位置是 AppDelegate 类,它的一个实例可以通过以下方式在其他类中获得:

MyAppDelegate *delegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];

then if the method is "doSomething" one accesses it by:

然后,如果该方法是“doSomething”,则可以通过以下方式访问它:

[delegate doSomething];

But perhaps this is too obvious or not what was required.

但也许这太明显了,或者不是所需要的。

回答by Hemant Singh Rathore

Another method that you can use

您可以使用的另一种方法

@interface ServerManager : NSObject

+(ServerManager *)getInstance;

@implementation ServerManager

+(ServerManager *)getInstance
{
static ServerManager *objServerManager = nil;

if(objServerManager==NULL){
objServerManager=[[self alloc] init];
}
// Return the servermanager object.
return objServerManager;
}

Call Whether you want to use

打电话 是否要使用

ServerManager *SMObject =   [ServerManager getInstance];

Don't forget to import servermanager.h file.

不要忘记导入 servermanager.h 文件。