calling a method of current class from a method of another class
I am working on a class A which has a method -(void)DoSmthing1. I am making a call to another method-(void)DoSmthing2 in class B. Now after doing some operations in class B, the method is supposed to call back a method-(void)DoSmthing3 previous class.
How will i call a method of current class from another class?? Can someone please help me....
Thanks in advance
edit1:: My Code: Class A
{
-(void) MethodA {
}
-(void) MethodB {
ClassB *clsB = [[ClassB alloc] init];
[clsB MethodC];
}
}
Class B
{
-(void)MethodC:(selector) {
//here i want to call MethodA of classA, and i will prefer if it is possible by sending the name of the method as selector in this method(methodC)
}
}
edit2::
Another example i want to do smthing like follwoing:
ClassB *b = [[ClassB alloc] nitWithTarget:self selector:@selector(methodfromClassA) object:nil];
Here i want to call a method of class A once some task in Class B is completed, and that too from class A.
I hope it is much clear now.
Edit3:
- (void)loadView {
AsyncConnection *async =[开发者_如何学编程[AsyncConnection alloc] init];
[async getAsync:self callback:@selector(test1)];
}
above code is from first class
-(void)getAsync:(id)anObject callback:(SEL)selector {
NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:anObject
selector:@selector(selector)
object:nil];
[queue addOperation:operation];
[operation release];
}
and above code is from second class. Here i want to call a method of first class which is passed as selector.
- (void)loadView {
AsyncConnection *async =[[AsyncConnection alloc] init];
[async getAsync:self callback:@selector(test1)];
}
Other class:
-(void)getAsync:(id)anObject callback:(SEL)selector {
NSInvocationOperation *operation = [[NSInvocationOperation alloc]
initWithTarget:anObject
selector:@selector(selector)
object:nil];
[queue addOperation:operation];
[operation release];
}
First, if you want to use the above pattern, remove the @selector()
from @selector(selector)
. You already have the selector. Doing @selector(selector)
will create the SEL
named selector
and not the selector passed in as the argument.
Next, this seems like an odd pattern to start with; it'll work, but any experienced Obj-C/iOS/Cocoa developer will get the willies looking at it. Since you wrote the AsyncConnection
class, use the delegate pattern. I.e. in the class that implements loadView
.
That is, in AsyncConnection.h:
@property(retain) ClassA *callbackHandler;
And then:
- (void)loadView {
AsyncConnection *async =[[AsyncConnection alloc] init];
[async setCallbackHandler: self];
}
Then, in AsyncConnection, you would refer to the callback handler directly as self.callbackHandler
(or [self callbackHandler]
).
It sounds like you would benefit from reading the introductory material: The Objective-C Programming Language and Cocoa Fundamentals Guide. Additionally, you should read up on the basics of object-oriented programming (there are dozens of overviews all over the web and Amazon).
Essentially, you're confusing a class (the blueprint for creating an object, in its most basic description) and an instance of a class (an actual "instantiated" object of a given class). Somewhere you're going to have to have a reference from one instance to another (like objectB = [[ClassB alloc] init]) to send a message (like [objectB doSomethingAnObjectOfClassBWouldDo]). You might accomplish this by storing the reference as an instance variable or inside a collection (array, dictionary, etc.) that is an instance variable of the class that needs to "remember" who it needs to talk to.
It's important to realize you're trying to walk before you've learned to crawl with this platform. The only cure is to study. A lot. Guided books help.
Then new approach: Should be something like:
classB.h
classA *aObj;
@property(nonatomic, retain) classA *aObj;
classB.m
@synthetize aObj;
classA.m
// after init class b obj
[bOjb setAObj:self];
classB.m
[aObj whateverMethodOfClassA];
Let me know if not clear.
My sugestion, is that generally if you want to avoid a real mess and a nightmare to debug, you should use observers and notifications rather than this kind of cross methods calls.
Make class A an observer of a notification of class B:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(classA-method-name:) name:@"notification-name" object:class-B-object ];
And when ready in class-B notify class A with parms:
[[NSNotificationCenter defaultCenter] postNotificationName:@"notification-name" object:self];
Just an idea.
One option is to use the delegate pattern in Objective C. Have the object of class A pass itself to an object of class B to be the handler for certain methods.
精彩评论