Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I don't understand your problem using delegates with a singleton pattern based class.</p> <p>You create a NSMutableArray to store the observers and notify all in a loop if something happened.</p> <pre><code>- (void)addObserver(id&lt;locationControllerDelegate&gt; observer) { [observers addObject: observer]; } - (void)notifyAll() { for (id&lt;locationControllerDelegate&gt; observer in observers) { [observer someMethod]; } } </code></pre> <p>Don't forget to add a removeObserver() Method.</p> <p>Than you can simply add the delegates via</p> <pre><code>[[MyClass sharedInstance] addObserver:self]; </code></pre> <p>In your case</p> <pre><code>[[LocationController locationmanager] addObserver:self]; </code></pre> <h1>Basic Example</h1> <p>So here a very basic (no memory management) code example, of how singleton works.</p> <p>Protocol: DelegateProtocol.h</p> <pre><code>#import &lt;Foundation/Foundation.h&gt; @protocol DelegateProtocol &lt;NSObject&gt; - (void)someMethod; @end </code></pre> <p>Singelton class:</p> <p>MySingelton.h</p> <pre><code>#import &lt;Foundation/Foundation.h&gt; @protocol DelegateProtocol; @interface MySingleton : NSObject{ NSMutableArray *observers; } + (MySingleton *)sharedInstance; - (void)addObserver:(id&lt;DelegateProtocol&gt;) observer; - (void)notifyAll; @end </code></pre> <p>MySingleton.m</p> <pre><code>#import "MySingleton.h" #import "DelegateProtocol.h" @implementation MySingleton static MySingleton *sharedInstance; - (id)init { self = [super init]; if (self) { observers = [[NSMutableArray alloc] init]; } return self; } + (MySingleton *)sharedInstance { if (sharedInstance == NULL) { sharedInstance = [[MySingleton alloc] init]; } return sharedInstance; } - (void)addObserver:(id&lt;DelegateProtocol&gt;)observer { [observers addObject:observer]; } - (void)notifyAll { for(id&lt;DelegateProtocol&gt; observer in observers) { [observer someMethod]; } } @end </code></pre> <p>And finally the class using the sharedInstance.</p> <p>SomeClass.h</p> <pre><code>#import &lt;Foundation/Foundation.h&gt; #import "DelegateProtocol.h" @interface SomeClass : NSObject &lt;DelegateProtocol&gt; @end </code></pre> <p>SomeClass.m</p> <pre><code>#import "SomeClass.h" #import "DelegateProtocol.h" #import "MySingleton.h" @implementation SomeClass - (id)init { self = [super init]; if (self) { } return self; } - (void)someMethod { NSLog(@"Called from singleton!"); } @end </code></pre> <p>And a main method that will use all this stuff:</p> <p>main.m</p> <pre><code>#import &lt;Foundation/Foundation.h&gt; #import "SomeClass.h" #import "MySingleton.h" int main (int argc, const char * argv[]) { NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; SomeClass *aClass = [[SomeClass alloc]init]; [[MySingleton sharedInstance] addObserver:aClass]; [[MySingleton sharedInstance] notifyAll]; [pool drain]; return 0; } </code></pre> <p>You will see that the someMethod-Method will be called on notifyAll.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload