Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>answer completely edited</strong></p> <p>Your question leads to multiple solutions and strategies.</p> <h3>First: general posibilities to implement bidirectional data-transfer between two classes.</h3> <p>via multiple protocols: loose cupling but leads to import-loops which are annoying. I know ho to solve import loops for class-definitions (@class) but I dont know how to solve this for protocols</p> <p>A.h:</p> <pre><code>#import "B.h" @protocol ADelegate -(void) adelegate:(NSString*)data; @end @interface A : NSObject&lt;BDelegate&gt; { id&lt;ADelegate&gt; delegate; } @end </code></pre> <p>B.h:</p> <pre><code>#import "A.h" @protocol BDelegate -(void) bdelegate:(NSString*)data; @end @interface B : NSObject&lt;ADelegate&gt; { id&lt;BDelegate&gt; delegate; } @end </code></pre> <p>via a single protocol: dense cupling :( but no import-loop (this is a working ugly style)</p> <p>A.h:</p> <pre><code>//no import here needed @protocol ADelegate -(void) adelegate:(NSString*)data; @end @interface A : NSObject&lt;BDelegate&gt; { id&lt;ADelegate&gt; delegate; } @end </code></pre> <p>B.h:</p> <pre><code>#import "A.h" @interface B : NSObject&lt;ADelegate&gt; { A* delegate; } @end </code></pre> <p>via pipe/stream: bidirectional data-transfer should by done using a pipe (unbuffered) or stream (buffered) here I show you a small and simple delegate-pipe but there also exists a NSPipe/NSStream</p> <p>DelegatePipe.h</p> <pre><code>@protocol DelegatePipeDelegate - dataArrived:(NSString*)data; @end @interface DelegatePipe : NSObject { NSMutableArray *delegates; } -(void)open:(id&lt;DelegatePipeDelegate&gt;)d; -(void)close:(id&lt;DelegatePipeDelegate&gt;)d; -(void)send:(NSString*)data; @end </code></pre> <p>DelegatePipe.m</p> <pre><code>@implementation DelegatePipe -(id)init { if(self = [super init]) { delegates = [NSMutableArray array]; } return self; } -(void) dealloc { [delegates release]; delegates = nil; } -(void) open:(id &lt;DelegatePipeDelegate&gt;)d { @synchronized(self) { if([delegates containsObject:d]) return; //if([delegates count]&gt;=2) //Pipe contains originally only 2 delegates. but a broadcaster is also nice ;) // return; [delegates addObject:d]; } } -(void) close:(id &lt;DelegatePipeDelegate&gt;)d { @synchronized(self) { [delegates removeObject:d]; } } -(void) send:(NSString *)data { @synchronized(self) { for(id&lt;DelegatePipeDelegate&gt; d in delegates) [d dataArrived:data]; } } @end </code></pre> <h3>Second: KVO</h3> <p>KVO is often used in a ModelViewController (MVC) Pattern. eg: visualize data in a view. The network-connection-state in your case is part of data and your loginipad is a view (<em>and a controller</em>)</p> <p>Authentificator.h</p> <pre><code>typedef enum eAuthState { NOT_CONNECTED = 0, LOGIN_FAILED, CONNECING, CONNECTED } AuthState; @interface Authentificator : NSObject { AuthState state; } @property (nonatomic, assign) AuthState state; @end </code></pre> <p>Authentificator.m</p> <pre><code>... -(void) doAuthWithUsername:(NSString*)name password:(NSString*)pw { self.state = CONNECING; //do network-stuff } //delegate from network. here NSURLConnection -(void) connectionDidFinishLoading:(NSURLConnection *)connection { //parse network-answer BOOL success = YES; if(success) self.state = CONNECTED; else self.state = LOGIN_FAILED; } </code></pre> <p>loginipad.h</p> <pre><code>@interface loginipad : UIViewController { Authentificator *auth; } @property (nonatomic, retain) Authentificator *auth; @end </code></pre> <p>loginipad.m</p> <pre><code>@implementation loginipad @synthesize auth; //override setter for more comfortable use (add/removeObserver) -(void) setAuth:(Authentificator *)a { @synchronized(auth) { [auth removeObserver:self forKeyPath:@"state"]; [auth release]; auth = a; [auth retain]; [auth addObserver:self forKeyPath:@"state" options:0 context:nil]; } } -(IBAction) buttonClicked:(id)aSender { self.auth = [Authentificator sharedAuthentificator]; } -(void) observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { if(![object isKindOfClass:Authentificator.class]) return; AuthState state = ((Authentificator*)object).state; NSLog(@"curState: %i",state); //do sth with state } - (void)dealloc { self.auth = nil; [super dealloc]; } </code></pre>
    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. This table or related slice is empty.
    1. 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