• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Using NSNotificationCenter passing an object or value?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Using NSNotificationCenter passing an object or value?


  • Subject: Re: Using NSNotificationCenter passing an object or value?
  • From: "Eric E. Dolecki" <email@hidden>
  • Date: Tue, 3 Aug 2010 22:38:33 -0400

You're right... mydata is indeed in a view.

I have a subclassed UIView to serve as my "component ui" - and it creates
instances of subclassed UIViews (containers) which in turn contain
subclassed UIButton items. Since I was being quickly lazy in prototyping, I
didn't include a view controller for any of these things... my main UIView
was the engine for the whole thing. When a user clicked on an item, the
button press was dispatched from the container view class back to the main
container class - so that layout code, etc. could happen.

Eric

On Tue, Aug 3, 2010 at 9:37 PM, Graham Cox <email@hidden> wrote:

>
> 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
>
>
>


--
http://ericd.net
Interactive design and development
_______________________________________________

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

References: 
 >Using NSNotificationCenter passing an object or value? (From: "Eric E. Dolecki" <email@hidden>)
 >Re: Using NSNotificationCenter passing an object or value? (From: Graham Cox <email@hidden>)

  • Prev by Date: Re: iOS: UITableViewCell an textlabel?
  • Next by Date: Re: How to delete read only files in LaunchAgent folder
  • Previous by thread: Re: Using NSNotificationCenter passing an object or value?
  • Next by thread: override hyperlink handler for WebView
  • Index(es):
    • Date
    • Thread