Using NSNotificationCenter passing an object or value?

2010-08-03 Thread Eric E. Dolecki
I am trying to pass along a custom UIButton's tag down to another class I am
writing.

In my custom view which contains the custom UIButton, I have this code *(A):
*

-(void) buttonClicked:(id)sender
{
 [[NSNotificationCenter defaultCenter]
postNotificationName:@ButtonClicked
object:sender];


In a class which manages these custom views, I have this code *(B):*


- (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;
}

-(void) buttonClicked {
//This works, but I need to know the tag
NSLog(@clicked);
}

Now, obviously in that I have object set to nil. How can I set this up? I
could change object to nil and use userInfo:someNSDictionary in the
customview which contains the UIButton... but I run into the same problem
in *(B). * I really haven't seen much in Google in regards to userInfo and
looking for some help on it. I understand some of how it works, not sure how
to set that *addObserver* part up properly.

Sorry for the noob question.
___

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


Re: Using NSNotificationCenter passing an object or value?

2010-08-03 Thread Quincey Morris
On Aug 3, 2010, at 12:34, Eric E. Dolecki wrote:

 -(void) buttonClicked:(id)sender
 {
 [[NSNotificationCenter defaultCenter]
 postNotificationName:@ButtonClicked
 object:sender];
 
 
 In a class which manages these custom views, I have this code *(B):*
 
 
 - (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;
 }
 
 -(void) buttonClicked {
//This works, but I need to know the tag
NSLog(@clicked);
 }
 
 Now, obviously in that I have object set to nil. How can I set this up? I
 could change object to nil and use userInfo:someNSDictionary in the
 customview which contains the UIButton... but I run into the same problem
 in *(B). * I really haven't seen much in Google in regards to userInfo and
 looking for some help on it. I understand some of how it works, not sure how
 to set that *addObserver* part up properly.

You *really* need to read the documentation. Your second buttonClicked  
method prototype is wrong -- it takes a NSNotification object as a parameter. 
If you had coded that properly, you would have a way of getting the 
notification's userInfo. (Or, to get the object and ask it for its tag. Or 
whatever.)

Your first buttonClicked method (why give them the same name?) can use 
'postNotificationName:object:userInfo:' if you really need to pass userInfo. 
(Again, you don't need to use userInfo if you just want the button's tag.)


___

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


Re: Using NSNotificationCenter passing an object or value?

2010-08-03 Thread Quincey Morris
On Aug 3, 2010, at 13:00, Eric E. Dolecki wrote:

 WHen you say it takes an NSNotification object as a parameter, you mean to 
 say that it's missing, correct?

Yes, as in:

- (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];
// note the colon in the selector now
   }
   return self;
}

-(void) buttonClicked: (NSNotification*) notification {
UIButton* button = notification.object;
NSUInteger tag = button.tag;
}
___

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


Re: Using NSNotificationCenter passing an object or value?

2010-08-03 Thread eric dolecki GMail
Many thanks. I didn't know object could be nil yet still pass it along when 
populated



On Aug 3, 2010, at 4:15 PM, Quincey Morris quinceymor...@earthlink.net wrote:

 On Aug 3, 2010, at 13:00, Eric E. Dolecki wrote:
 
 WHen you say it takes an NSNotification object as a parameter, you mean to 
 say that it's missing, correct?
 
 Yes, as in:
 
 - (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];
 // note the colon in the selector now
   }
   return self;
 }
 
 -(void) buttonClicked: (NSNotification*) notification {
   UIButton* button = notification.object;
   NSUInteger tag = button.tag;
 }
 ___
 
 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/edolecki%40gmail.com
 
 This email sent to edole...@gmail.com
___

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


Re: Using NSNotificationCenter passing an object or value?

2010-08-03 Thread Quincey Morris
On Aug 3, 2010, at 13:50, eric dolecki GMail wrote:

 Many thanks. I didn't know object could be nil yet still pass it along when 
 populated

I *knew* you were going to say exactly that. :)

Specifying nil for the 'object:' parameter just means (and I quote from the 
NSNotificationCenter reference) the notification center doesn’t use a 
notification’s sender to decide whether to deliver it to the observer. All 
notifications still have a sender, available as the 'object' property of the 
notification object.

If the parameter had been named something like 'objectFilter:' instead, you 
wouldn't have been confused. As it is, reading the documentation would have 
cleared up the apparent ambiguity, which is why I keep harping on READING THE 
DOCUMENTATION.


___

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


Re: Using NSNotificationCenter passing an object or value?

2010-08-03 Thread Graham Cox

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


Re: Using NSNotificationCenter passing an object or value?

2010-08-03 Thread Eric E. Dolecki
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 graham@bigpond.com 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 (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