开发者

Send an event to call a method between two classes

How can I invoke a method in a class only after verifying a condition in another method of another class in my iPhone app?

Any ideas开发者_Python百科?

Thanks, Andrea

edit 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

 }

}

hope this help to understand (even better) the problem...


You need to use delegation. Making class 1 class 2's delegate lets class 2 send messages to class 1.

Edit changes: You want class2 to be the delegate of class 1. This means that class 1 will tell class 2 to perform method1 with whatever comes after after the colon. This can be any object. In the example, I used a string. Process method1 as you normally do, but remember that the xmlFile variable should be used instead of a hardcoded object, i.e. use xmlFile instead of @"file1.xml".

EDITED Example:

class 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

Synthesize delegate in your .m

Then call [delegate method1:@"file1"].

class 2 .h:

#import "class1.h"

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

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜