Re: Using NSNotificationCenter passing an object or value?
Re: Using NSNotificationCenter passing an object or value?
- Subject: Re: Using NSNotificationCenter passing an object or value?
- From: Graham Cox <email@hidden>
- Date: Wed, 4 Aug 2010 11:37:01 +1000
On 04/08/2010, at 5:34 AM, Eric E. Dolecki wrote:
> -(void) buttonClicked:(id)sender
> {
> [[NSNotificationCenter defaultCenter]
> postNotificationName:@"ButtonClicked"
> object:sender];
>
Notwithstanding Quincey's excellent advice, this approach is poor use of MVC.
The Button is a view. The thing that responds to the button is the controller. At that point, the button should have done its job and is no longer a relevant object in the design. WHAT DOES THE BUTTON DO? This is the question the controller needs to be asking itself, and messaging the data model appropriately, not just punting the button object somewhere else for somebody else to make that call (clue: if ever you send a notification and the object parameter is not 'self', you need to asking hard questions about why that is). The controller is not doing its job here, and you've made an unnecessary link between view and model.
> - (id)initWithFrame:(CGRect)frame {
> if ((self = [super initWithFrame:frame])) {
> mydata = [[NSArray alloc] init];
> menuItems = [[NSMutableArray alloc] initWithCapacity:40];
> [[NSNotificationCenter defaultCenter] addObserver:self
> selector:@selector(buttonClicked) name:@"ButtonClicked" object:nil];
> }
> return self;
> }
This also suggests you've embodied your model (mydata) in a view. It's one reason you're resorting to using notifications to perform basic messaging - nobody is really clear about their correct role here.
If you can separate your design into clearly defined model, view and controller layers, things will be far easier. Notifications have a useful part to play but you're abusing them here in order to band-aid a muddled design.
General documentation about MVC should be a vital read at this point.
--Graham
_______________________________________________
Cocoa-dev mailing list (email@hidden)
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:
This email sent to email@hidden