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:

-(void)b_method
{
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

    Error

    default userpic

    Your IP address will be recorded 

  • 3 comments

Deleted comment

hattifattener

March 26 2009, 03:59:40 UTC 5 years ago

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?

carlogulliani

March 26 2009, 08:20:40 UTC 5 years ago

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

carlogulliani

March 27 2009, 06:41:20 UTC 5 years ago

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
{
NSLog(@"%@",str);
}

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