0

With the help of good people here, i was able to write a singleton but cant get the delegates from it.

At the start i had a socketConnector class and from the Home class, i made an instance of him, and also got a delegates from him :

       socketInstance=[[SocketConnector alloc]init]; //in Home class
        socketInstance.delegate=self;

Than i needed to create a singleton for him, and STILL get the delegates from him to Home.

so i created a singleton class name : sharedConnector and in it i have :

+(sharedConnector*)defaultConnector
{
    static  sharedConnector   *myConnector ;
    @synchronized(self)
    {
        if( myConnector==nil  )
            myConnector=[[sharedConnector alloc]init ];
        return myConnector;
    }   
}

    - (id)init
    {
        if (self = [super init])
        {
            connector=[[SocketConnector alloc]init]; //setting here the socketClass
        }
        return self;
    }

as you see i am setting here the socket class once , than to use it from Home class:

    [sharedConnector defaultConnector].connector.delegate=self; //setting delegates to home!

(connector has a property on the singleton) it do work , but i dont get the delegates to home anymore . (all the delegates functions are still in Home class as they where at the start )

Curnelious
  • 1
  • 16
  • 76
  • 150
  • do you want to invoke delegate method from Singleton? – Dipen Panchasara Mar 27 '13 at 13:28
  • i want to get the delegates to the Home class, using the singleton, as i showed i was trying to add the delegate of the connector instance from the singltone to the home class.. – Curnelious Mar 27 '13 at 13:33
  • case it's singleton, the init method is not invoke,when you use these code:[sharedConnector defaultConnector].connector.delegate=self; when did you invoke the init method of the singleton class ? – junkor Mar 27 '13 at 15:00
  • @junkor it is invoked. [sharedConnector defaultConnector] calls the init method on sharedConnector which in turn calls the init method of the connector object – giorashc Mar 28 '13 at 09:58

1 Answers1

0

You are accessing connection directly which is NOT the same as self.connection (which uses the synthesized method).

So change :

connector=[[SocketConnector alloc]init]; 

to

self.connector=[[SocketConnector alloc]init]; 

There is a good explanation of why here

Community
  • 1
  • 1
giorashc
  • 13,691
  • 3
  • 35
  • 71