On Oct 7, 2010, at 11:58, Hans van der Meer wrote: > In my application I instantiate a MainController (the delegate having > "awakeFromNib) from where all actions are dispatched. In order to send > messages to the displaywindow residing in MainController, there is the > problem how to find this MainController from other callers (possibly on other > threads). Now it is coded as follows: > > In MainController.c: > static MainController *controller = nil; > controller = self; /* in its init method */ > + (void) message:(NSString *)msg; > { dispatch_async(dispatch_get_main_queue(), ^{[controller message:msg];});} > - (void) message:(NSString *)msg; > > I am not very happy with the static variable but do not see another way to > accomplish this. I fear my solution is not as much in the spirit of Cocoa > programming as it should be. > Is there a better way?
Is MainController your application delegate, or is it a window controller? (It could be both at the same time, too.) If it's your application delegate, other objects can find it as [NSApp delegate] or [[NSApplication sharedAppplication] delegate]. If it's just a window controller, add a "mainWindowController" property to your application delegate, and refer to it as [[NSApp delegate] mainWindowController]. OTOH, a singleton pattern as you've sort of done already *is* a valid solution. However, it's often the case with questions like yours that the difficulty is really a conceptual design issue, and that you might be torturing the frameworks classes or the MVC design paradigm. (Passing a pointer to a window controller seems unlikely for a dispatched Block, for example.) _______________________________________________ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com