发送一个事件来调用两个类之间的方法

时间:2010-09-10 10:47:25

标签: iphone objective-c events

如何在我的iPhone应用程序中验证另一个类的另一个方法中的条件后,如何在类中调用方法?

有什么想法吗?

谢谢,Andrea

编辑3

//class1 

//Class1.m


@implementation Class1 {

 ....

    [class2  method1:@"file1.xml"];

    [class2  method1:@"file2.xml"];

    [class2  method1:@"file3.xml"];
} 
        ….

  @end

  //class2

#import "Class1.h"  


@implementation Class2{

-(void) method1(NSString *)file{

   [self method2];

 }


-(void) method2{

   //when finish that method I have to call the successive method [class2  method1:@"file2.xml"]; in class1

 }

}

希望这有助于理解(甚至更好)问题......

1 个答案:

答案 0 :(得分:0)

您需要使用委托。使class 1 class 2的委托允许class 2将消息发送到class 1。

编辑更改:您希望class2成为类1的委托。这意味着类1将告诉第2类使用冒号之后的任何内容执行method1。这可以是任何对象。在示例中,我使用了一个字符串。像往常一样处理方法1,但要记住应该使用xmlFile变量而不是硬编码对象,即使用xmlFile而不是@“file1.xml”。

已编辑示例:

1级.h:

#import <UIKit/UIKit.h>
..etc

//a protocol declaration must go before @interface
@protocol class1Delegate
-(void)method1:(NSString *)xmlFile;
@end


@interface class1 {
 id <class1Delegate> delegate;
}

@property (nonatomic, assign) id <class1Delegate> delegate;
@end

合成.m

中的委托

然后拨打[delegate method1:@"file1"]

2级.h:

#import "class1.h"

@interface class2 <class1Delegate> {
//put whatever here
}

- (void)method1:(NSString *)xmlFile;