carlogulliani (carlogulliani) wrote in macosxdev,

reference between classes

 Hello, i have 2 classes: A and B.

I wanna call some B's method from A.

class A:

- (IBAction) callmethod:(id)sender
A *a_method = [[A init] alloc];
[a_method performSelector: @selector(b_method) withObject: nil afterDelay: 2.0];
[a_method release];

class B:

NSLog(@"b_method was called");
NSNetService * rs = [service objectAtIndex:0];
rs.delegate = self;
[rs resolveWithTimeout:10];

after it should calling onSocket

-(void)onSocket:(AsyncSocket *)sock didConnectToHost:(NSString *)host port:(UInt16)port
NSLog(@"did connect to host");

but i've not result, i have just 'b_method was called' this method was not called

but if i'll add IBAction's method to B class:

class B
-(IBAction) buttonpressed:(id)sender
[self b_method];

then i have 'did conntct to host in debug mode'

  • Post a new comment


    default userpic

    Your IP address will be recorded 

    When you submit the form an invisible reCAPTCHA check will be performed.
    You must follow the Privacy Policy and Google Terms of use.

Deleted comment

It's also failing because [[A init] alloc] is wrong. You need to allocate a new instance of A, and then initialize it.

"performSelector attempts to call a message on the object that CALLS it"

Not exactly, it sends the message to the object you invoke it on (the receiver). It doesn't matter what object (if any) calls it.

Another thing to notice is that there's no mention of B in A's callmethod: implementation, so there's no way for the system to know that some instance of B is what's supposed to receive that message (and perform the b_method method). Maybe you intended to create an instance of B there?
wow, how all is difficult... About NSNotification i all understood and i thought already about it, but can you tell me more about allocating new instance of A and initialize it?

thanks in advance

Deleted comment

ow, thanks, but, it seems, it like on my example. So, i solved it with help NSNotification*, but i wanna understand else one way of solution for the future.

btw, when i used NSNotificanion i've found the bug:

// i have NSString variable in header file
// ClassA.h
NSString *str;

// in ClassA.m i have the next method
// in the other class i sending notification with object:@"hello"
-(void) mymethod:(NSNotification *)notification
str = (NSString *)[notification object];

// and in classA i have some IBAction

-(IBAction) pressme:(id)sender

but always i've got "EXC_BAD_ACCESS"

//i added label in IB with outlet 'mylabel' and replaced method:
-(void) mymethod:(NSNotification *)notification
[mylabel setText:(NSNotification *)[notification object]];

-(void) pressme:(id)sender
NSLog(@"%@",[mylabel text]);

//it works. full task: i should get notification with object and then to check notification object with my some variable