Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Google Sign In with OpenID

about (id) sender

bqbqhahabqbqhaha Posts: 63Registered Users
edited March 2009 in iPhone SDK Development
Hi, guys

I have a question about (id) sender..

- (IBAction) tapStopButton;

or

- (IBAction) tapStopButton: (id) sender;
...

It seems that - (IBAction) tapStopButton: (id) sender; is much used...
But, can I just use - (IBAction) tapStopButton;??

What is different btw
- (IBAction) tapStopButton; and
- (IBAction) tapStopButton: (id) sender; ??

What is (id) sender for???
Post edited by bqbqhaha on

Replies

  • mattjgallowaymattjgalloway Posts: 250Registered Users
    edited March 2009
    (id)sender is the object which sent the message to that selector. It's like in the delegate functions where you have the table view passed in to the cellForRowAtIndexpath function, etc.

    You'd use this if you had 2 objects which were calling that selector and you wanted to distinguish between them. Of course, you could just use two different functions, but it's often cleaner and less duplication of code to use one function.
    <a href="http://bit.ly/BeardBoothHD" target="_blank">Beard Booth HD</a> - See what you'd look like with a beard today!<br />
    <br />
    <a href="http://iphone.galloway.me.uk/" target="_blank">My iPhone Apps</a><br />
    <a href="http://iphone.galloway.me.uk/i
  • jonesyyyyjonesyyyy Posts: 9New Users
    edited March 2009
    I recently read about this in an iphone development book and the example they use is a view with 2 buttons (left and right) and 1 label.

    The controller only has one method -(IBAction) buttonPressed:(id)sender; which is used by both buttons. The sender object is used to get the button name and then updates the label with the string "<buttonName> was pressed".

    so if u call your left button "left" and right button "right" you will get:
    "Left button pressed" when button named left is pressed
    "Right button pressed" when button name right is pressed

    An action method doesnt have to pass the send object through, you can leave it off if you dont need it. It does come in usful in this example as now we only need one method to handle any button press. It saves us writing 2 methods rightButtonPressed and leftButtonPressed and hardcoding the new string to pass to the label.


    This is my understanding and being a newbie myself i may be wrong, so please feel free to correct me anyone
  • PhoneyDeveloperPhoneyDeveloper Posts: 1,431Registered Users
    edited March 2009
    Look at the UIControl class reference for a description of action methods.
Sign In or Register to comment.