views:

39

answers:

1

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?

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...

A: 

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;
MishieMoo
Thanks.But if I have to change the parameter passed to method1 every time I have to re-invoke it, Have I to insert every call into the doMethod1 method?
Andrea Mattiuz
I've re-edited the answer
Andrea Mattiuz
Are file1 file2 etc objects or parameters? I.e. your function calls method1:@"name" file1:@"something" or is file1 passed into method1?
MishieMoo
file1, file2 etc are parameters. I.e. [class2 method1:@"file.xml"];
Andrea Mattiuz
So am I right in thinking that you call method1 then method2, and then restart with the next file?
MishieMoo
Yes you're right!
Andrea Mattiuz
Edited! Please let me know if I'm explaining things well and if I got the gist of what you're doing.
MishieMoo
Thank you so much for your help. I'll try it as soon as possible, unfortunately not before monday. Sorry
Andrea Mattiuz
nothing working... re-edit to explain the problem as better I can the problem.
Andrea Mattiuz
I solve the problem changing the position from where I call the methods. Thank you anyway for your help, you explained well your way to solve the problem, maybe I wasn't able to catch that enaugh.
Andrea Mattiuz