简体   繁体   English

iPhone SDK:访问其他类中的方法

[英]iPhone SDK: Accessing methods in other classes

In my iPhone application I have multiple class files, I have my main application's class files, then I have my UIView class files. 在我的iPhone应用程序中,我有多个类文件,有我的主应用程序的类文件,然后有我的UIView类文件。 I have a simple -(void) method declared in my UIView class files, how can I access it from my main applications class files? 我在UIView类文件中声明了一个简单的-(void)方法,如何从主应用程序类文件中访问它?

A bit more detail: In my application a video is played, when this video finishes playing a notification is sent and actions are preformed, which I have already successfully set up, however when the movie finishes I would like a method declared in another class file to be preformed. 更详细一点:在我的应用程序中,播放了一个视频,当该视频播放完毕后,便会发送通知并执行操作(我已经成功设置了该操作),但是当电影播放完后,我希望在另一个类文件中声明一个方法待执行。 If the method was declared in the same class file I would simply use this code: [self mySimpleVoidMethod]; 如果在同一个类文件中声明了该方法,则只需使用以下代码:[self mySimpleVoidMethod]; But obviously this doesn't work If the method is declared in a different class file. 但是,如果在不同的类文件中声明了该方法,则显然不起作用。 I believe it is possible to access a method declared in a different class file, but I just haven't got a clue about how to do it. 我相信可以访问在其他类文件中声明的方法,但是我只是不知道如何执行此操作。 Sorry if I'm using completely incorrect terms to name things. 抱歉,如果我使用完全不正确的术语来命名。 But I am relatively new to programming all together. 但是我对一起编程还是比较陌生的。

You've got a couple of options, depending on your setup. 根据您的设置,您有几种选择。 Here are a few: 这里有一些:

1) Add a reference to the class with the function (the callee) as a property in the caller's class: 1)添加对具有功能(被调用方)的类的引用作为调用者类中的属性:

Caller.h Caller.h

@interface Caller : SomeObject {
    Callee *myCallee;
    ...
}
@property(nonatomic, retain) Callee *myCallee;

Caller.m Caller.m

@synthesize myCallee;
-(void)someAction {
    [myCallee doSomething];
}

Something that sets up Caller after initializing both classes: 在初始化两个类之后设置Call​​er的东西:

caller.myCallee = callee;

2) Use another notification event, like it looks like you already know how to do. 2)使用另一个通知事件,看起来您已经知道该怎么做。

3) Use a protocol if you've got a bunch of different classes that Caller might need to call that all support the same method: 3)如果您有很多调用者可能需要调用的,支持相同方法的不同类,请使用协议:

DoesSomething.h DoesSomething.h

@protocol DoesSomething
    -(void)doSomething;
@end

Callee.h Callee.h

@interface Callee : NSObject<DoesSomething> {  // NSObject or whatever you're using...
    ...
}
-(void)doSomething;

Caller.h Caller.h

@interface Caller : SomeObject {
    id<DoesSomething> *myCallee;
    ...
}
@property(nonatomic, retain) id<DoesSomething> *myCallee;

... Then as per example 1. ...然后按照示例1。

4) Use performSelector to send a message to the class. 4)使用performSelector向该类发送消息。

Caller.h Caller.h

@interface Caller : NSObject {
    SEL action;
    id callee;
}
-(void)setupCallbackFor:(id)target action:(SEL)callback;

Caller.m Caller.m

-(void)setupCallbackFor:(id)target action:(SEL)callback {
    callee = target;
    action = callback;
}

-(void)someAction {
    if([callee respondsToSelector:action]) {
        [callee performSelector:action];
}

I'm sure there are other ways, and there are pros and cons to each of these, but something in there should fit your needs and/or give you enough to scan the documentation to fill in any gaps... 我敢肯定还有其他方法,每种方法都有优点和缺点,但是其中应该有适合您的需求和/或给您足够的空间来扫描文档以填补任何空白...

I did a blog post a few weeks ago that outlines one way to do this. 几周前,我写了一篇博客文章,概述了执行此操作的一种方法。 It is similar to the previous answers, and includes some sample code you can download and look at. 它与先前的答案类似,并且包含一些示例代码,您可以下载并查看它们。 It is based on using table view controllers, but you should be able to adapt the ideas to your application without too much difficulty. 它基于使用表视图控制器的原理,但是您应该能够轻松地将其思想适应您的应用程序。

Passing values and messages between views on iPhone 在iPhone上的视图之间传递值和消息

You'll need an instance of the other class, accessible from the code that runs when the movie finishes. 您需要另一个类的实例,可以从电影完成时运行的代码中访问该实例。 Often, this is accomplished by storing an instance of the other class as a field in the class, set either via a "setter", or during construction. 通常,这是通过将另一个类的实例存储为类中的字段(通过“设置器”或在构造过程中进行设置)来实现的。 You could also use key-value observing , watching a key representing the playstate of the movie; 您还可以使用键值观察 ,观看代表电影播放状态的键; an instance of the other class can register to observe the changes to this key. 另一个类的实例可以注册以观察对此键的更改。

Specifically for patterns using UIView, your UIViewController for the view will have access to it (through the view method). 专门针对使用UIView的模式,视图的UIViewController可以访问它(通过view方法)。 If your "main application's class files" have a pointer to the controller - which they probably will, setup via Interface Builder - then that's an easy way to get to a UIView instance. 如果您的“主应用程序的类文件”具有指向控制器的指针(可能会通过Interface Builder进行设置),则这是获取UIView实例的简便方法。

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

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