Jump to content
Sign in to follow this  
Followers 0

pass argument to button's action method problem

4 posts in this topic

Recommended Posts

i'm making a "numpad" gui in xcode3 for iphone os, and i need each number button to append its respective digit to a string. apple's "hello world" tutorial executes this statement to attach the "hello" method to a button.


 [button addTarget:self action:@selector(hello:) forControlEvents:UIControlEventTouchUpInside];


i wrote this method to append the string:

 - (void)addDigitToBarCode: (NSString *)numstr
 NSString *barcodeString = [barcodeString stringByAppendingFormat:@"",numstr]; //add digit to string
 self.label.text = barcodeString;


but when I run this statement:

 [button9 addTarget:self action:@selector(addDigitToBarCode:"9") forControlEvents:UIControlEventTouchUpInside];


I get a warning:

 error syntax error before string constant


so what's the correct way to pass an argument to the method? thanks in advance

Share this post

Link to post
Share on other sites

I assume all your buttons have text like "1", "2", "3" etc on them?


Then you'd better make on event handler for all your numeric buttons, and then append the buttons text to your string.


Something like this:


- (void)buttonEventhandler: (id)sender {
  barcodeString = [barcodeString stringByAppendingString: [sender titleForState: [sender state]]];

Share this post

Link to post
Share on other sites

Arguments aren't meant to be sent using the method outlined in the first post. Not only that, but if that were how to do it, it should have been @"9", not just "9" (pointer).


PascalW's method is probably the best way to do it with the least lines of code (can't get much more compact than 1 line).

Share this post

Link to post
Share on other sites

Dusting off this old topic to show that I cared enough to search... ;)


Here is a similar problem....


Imagine an iPhone app with a standard iPhone table list of anyway from 10 to 1,000+ entries.

Imagine having an 'On/Off' switch attached to each of those XXXX entries


How would I build my code so I could call a single selector and be able to identify the 'table row' the switch was on.



For example


Apple     [Off/On]
Banana  [Off/On]
Peach    [Off/On]
Pear      [Off/On]
Plumb    [Off/On]


When I click on the Off/On toggle button for the peach it goes to a single action:@selector(hitTheBotton:) and in turn that procedure would need to unravel that it was called *not only* because an Off/On button was pressed but the specific table row. This must be doable otherwise what would be the point of including buttons in a table of items... Again... since the actual number of items could be anywhere from 10 to 1000 writing a unique procedure for each button simply wouldn't be possible or in any way an acceptable solution.


I'm still very new to all of this and with a bit of a forced break away from SDK work (last worked with rel. 2) I'm more confused than ever and the web is populated with TONS of example of things that... do work... was broke/changed in rel 6... was broke/changed in rel 5... was broke/changed in rel 4... was broke/changed in rel 3... etc etc etc...


In my google aided searches I saw some cryptic reference to using a 'tag' and or naming each button... I can't find any hardcore working examples of such things but if I were to be able to 'name' or 'tag' the button with the 'table row number' at the time the table is being built (and button was being attached) that would be okay...


Anyone have any currently working ideas? ;)




Well after going over all the available docs example code postings etc etc etc.... I've given up on using the fancy 'On/Off' switch that the SDK provides and instead have chosen to simply use the basic button type and utilize several fields (that for me are unused) for storing the table row number for each button.



Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  
Followers 0

  • Recently Browsing   0 members

    No registered users viewing this page.