Is "(id)sender" necessary in "-showInfo:" method?


This is kind of a tangential question, but when we programmatically create the UIBarButtonItem to show the RSSChannel info, the method it sends its target is:

- (void)showInfo:(id)sender

Why does this method include (id)sender as an argument when it works just well without (id)sender? I’ve only seen (id)sender used with -(IBAction) methods called by buttons created in Interface Builder.


The sender field doesn’t appear to be used at this point in this method. However, he may have included this in preparation for another chapter. Rather than rewriting the code the author went ahead and included it now.

As far as sender goes, it is just a name. There is nothing special about the name. The type, id, is generic and so could represent many different things. Thus his code is set up to handle a variety of situations.



If you watch Prof. Paul Hegarty’s iPad and iPhone Application Development (class #9 “Table Views” – link below), from Stanford, you will see that they ask him the very same question.

According to him, it is just considered to be “good practice” for the sender to send himself along the message, so that the receiver can message back in case it needs more info from the sender. He is talking about delegation specifically, but I imagine it is the same for target-action messaging like in this case.

So, it is apparently just a common design pattern that gets repeated over and over to account for all different case scenarios. In our particular case here, we will never message the sender back, so we can remove (id)sender completely if we want.

Here’s the link for that iTunes U video (free). The answer you are looking for is @ 1:06:00.