2010-09-10 74 views
0

如何在我的iPhone应用程序中验证另一个类的另一个方法中的条件后才能在类中调用方法?发送事件以调用两个类之间的方法

任何想法?

感谢,安德烈

编辑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 

} 

} 

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

+0

出了什么问题只是发送消息的正常方式[someObj中someMessage]? – 2010-09-10 10:57:10

+0

我必须确定该方法已经完成其执行 – 2010-09-10 12:31:29

+0

我编辑了这个问题......希望它更清楚.. – 2010-09-10 12:55:53

回答

0

你需要使用委派。制作1个2级的委托让2级将消息发送到1级

编辑变化:你想Class2中为1级的代表这意味着1级会告诉类2,无坚不摧执行方法1在结肠之后。这可以是任何对象。在这个例子中,我使用了一个字符串。 Process method1与通常一样,但请记住应该使用xmlFile变量而不是硬编码对象,即使用xmlFile而不是@“file1.xml”。

EDITED例子:

类1 .H:在您的m

#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 

合成委托然后调用[delegate method1:@"file1"]

类2·H:

#import "class1.h" 

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

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

Thanks.But如果每次必须重新调用传递给method1的参数,我是否需要将每个调用插入到doMethod1方法中? – 2010-09-10 13:41:37

+0

我已经重新编辑了答案 – 2010-09-10 14:22:41

+0

是file1 file2 etc对象还是参数?即你的函数调用method1:@“name”file1:@“something”或file1传递给method1? – MishieMoo 2010-09-10 15:24:21